1. 属性操作
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ../jQuery.min.js" > </ script>
</ head>
< body>
< a index = " how" data-index = " XingWei" href = " http://www.baidu.com" title = " Hello" > 百度</ a>
< input type = " checkbox" name = " " id = " " >
< span> 123</ span>
< img src = " " alt = " " >
< img src = " " alt = " " >
< img src = " " alt = " " >
< script>
$ ( function ( ) {
console. log ( $ ( "a" ) . prop ( "href" ) ) ;
$ ( "a" ) . prop ( "title" , "BaiDu" ) ;
$ ( "input" ) . change ( function ( ) {
console. log ( $ ( this ) . prop ( 'checked' ) ) ;
} ) ;
console. log ( $ ( "a" ) . attr ( "index" ) ) ;
console. log ( $ ( 'a' ) . attr ( 'data-index' ) ) ;
console. log ( $ ( "a" ) . prop ( "index" ) ) ;
$ ( "img" ) . attr ( { src : "test.png" , alt : "this is a img;" } )
$ ( "span" ) . data ( "uname" , "andy" ) ;
console. log ( $ ( "span" ) . data ( "uname" ) ) ;
console. log ( $ ( 'a' ) . data ( 'index' ) ) ;
} ) ;
</ script>
</ body>
</ html>
2. 内容文本值
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ../jQuery.min.js" > </ script>
</ head>
< body>
< div id = " id0" >
< span> 我是内容</ span>
</ div>
< div id = " id1" > test</ div>
< input type = " text" value = " 请输入内容" >
< div id = " id2" > 测试</ div>
< div id = " id3" > < p> 含有标签测试</ p> </ div>
< script>
console. log ( $ ( "#id0" ) . html ( ) ) ;
$ ( "#id0" ) . html ( "Hello World" ) ;
console. log ( $ ( 'div' ) . html ( ) ) ;
console. log ( $ ( "#id1" ) . text ( ) ) ;
$ ( "#id1" ) . text ( "World" ) ;
console. log ( $ ( "input" ) . val ( ) ) ;
$ ( "input" ) . val ( 123 ) ;
console. log ( '--------------' ) ;
console. log ( $ ( "#id2" ) . html ( ) ) ;
console. log ( $ ( "#id2" ) . text ( ) ) ;
console. log ( "---------" ) ;
console. log ( $ ( "#id3" ) . html ( ) ) ;
console. log ( $ ( "#id3" ) . text ( ) ) ;
</ script>
</ body>
</ html>
3. parent系列
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ./jQuery.min.js" > </ script>
</ head>
< body>
< div class = " one" >
< div class = " two" >
< div class = " three" >
< div class = " four" > 我不容易</ div>
</ div>
</ div>
</ div>
< script>
console. log ( $ ( ".four" ) ) ;
console. log ( $ ( ".four" ) . parent ( ) . parent ( ) . parent ( ) ) ;
console. log ( $ ( ".four" ) . parents ( ) ) ;
console. log ( $ ( ".four" ) . parents ( ".one" ) ) ;
</ script>
</ body>
</ html>