dom对象与jQuery对象的区别
$ ( function ( ) { var $txt = $ ( ":input" ) ; var txt = $txt[ 0 ] ; txt = $txt. get ( 0 ) ; console. log ( txt. value) ; txt = document. getElementById ( "in1" ) ; $txt = $ ( txt) ; console. log ( $txt. val ( ) ) ; } ) ;
设置css样式
< style type= "text/css" > . div1 { font- size: 80 px; } . color { color: pink; } < / style> < script src= "js/jquery-3.2.1.js" > < / script> < script> $ ( function ( ) { $ ( "div" ) . css ( { "background" : "yellow" , "border" : "solid 1px red" } ) ; alert ( $ ( "div" ) . css ( "border" ) ) ; $ ( "div" ) . prop ( "id" , "1" ) ; alert ( $ ( "div" ) . prop ( "id" ) ) ; $ ( "div" ) . height ( 180 ) ; alert ( $ ( "div" ) . height ( ) ) ; $ ( "div" ) . addClass ( "div1" ) ; $ ( "div" ) . removeClass ( "div1" ) ; $ ( "#btn" ) . click ( function ( ) { $ ( "div" ) . toggleClass ( "div1 color" ) ; } ) ; < / script>
属性操作
alert ( $ ( "div" ) . prop ( "title" ) + "----" + $ ( "div" ) . attr ( "title" ) ) ; alert ( $ ( "div" ) . prop ( "count" ) + "----" + $ ( "div" ) . attr ( "count" ) ) ; $ ( "#div1" ) . html ( "<input/>" ) ; alert ( $ ( "#div1" ) . html ( ) ) ; $ ( "#div1" ) . text ( 123 ) ; alert ( $ ( "#div1" ) . text ( ) ) ; alert ( $ ( "#in1" ) . val ( ) ) ; $ ( "#in1" ) . val ( "789" ) ;
文档操作
$ ( "select" ) . append ( "<option value=0>兰州</option>" ) ; $ ( "<option value=1>常德</option>" ) . appendTo ( "select" ) ; $ ( "select" ) . prepend ( "<option value=2>东莞</option>" ) ; $ ( "<option value=3>新疆</option>" ) . prependTo ( "select" ) ; / $ ( "select" ) . after ( "<input />" ) ; $ ( "select" ) . empty ( ) ; } ) ;