Home >Web Front-end >HTML Tutorial >Xiaoqiang's road to HTML5 mobile development (36) – DOM operations in jQuery
1. Query
Use the selector to find the node
Use html() / text() / attr() to output the node text and attribute value.
Note: Use val()
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script> <script> $(function(){ $('#b1').click(function(){ //$('#d1').html('java'); //将节点的属性读出来 //$('#d1').attr('style'); //$('#d1').attr('style','font-size:30pt'); $('#d1').attr('class','s1'); }); }); </script> <style> .s1{ color:red; } </style> </head> <body> <div id="d1">hello</div> <ul> <li>item1</li> <li id="i1">item2</li> <li>item3</li> </ul> <input type="button" id="b1" value="点我"/> </body> </html>
2 for drop-down list, create
$(html)
3, insert node
append();
prepend();
after();
before();
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script> <script> $(function(){ $('#b1').click(function(){ var $node = $('<li>item4</li>'); $('ul').append($node); //$('ul').append('<li>item4</li>'); 和上面是等价的 }); }); </script> <style> .s1{ color:red; } </style> </head> <body> <div id="d1">hello</div> <ul> <li>item1</li> <li id="i1">item2</li> <li>item3</li> </ul> <input type="button" id="b1" value="点我"/> </body> </html>
4. Delete node
remove();
remove(selector);
empty();Clear content
$('#b1').click(function(){ //$('ul li:eq(1)').remove(); $('ul li').remove('li[id=i1]'); $('ul li:eq(1)').empty(); });
5. Copy node
clone();
clone(true); Make the copied node also have behavior
6. Attribute operation
Read: attr(' ');
Setting: attr(' ', ' ');
Or set multiple attributes attr({" ", " "});
Delete: removeAttr(' ');
$('#b1').click(function(){ $('#d1').attr({"class":"s1","style":"font-size:40pt"}); });
7. Style operation
Get and set: attr("class", " ");
Append: addClass(' ', ' ');
Switch styles: toggleClass(' ', ' ');
Whether there is a certain style hasClass(' ');
css(' ', ' ');
css({ ' ': ' ', ' ': ' '});
$('#b1').click(function(){ $('div:first').addClass('s1 s2'); $('div:first').removeClass('s2'); $('div:first').toggleClass('s1'); });
8. Set and get html, text and value
html() / html(' ')
text() / text(' ')
val(); Set and read the value of the element
9. Traverse
children()
next();
prive();
siblings(): all brothers
10, comprehensive example
<script> $(function(){ $('#b1').click(function(){ //$('#d1').html('java'); //将节点的属性读出来 $('#d1').attr('style'); $('#d1').attr('style','font-size:30pt'); $('#d1').attr('class','s1'); }); $('#b1').click(function(){ var $node = $('<li>item4</li>'); $('ul').append($node); //$('ul').append('<li>item4</li>'); 和上面是等价的 }); $('#b1').click(function(){ //$('ul li:eq(1)').remove(); $('ul li').remove('li[id=i1]'); $('ul li:eq(1)').empty(); }); $('#b1').click(function(){ var $node = $('ul li:eq(2)').clone(); $('ul').append($node); var $node = $('ul li:eq(2)').clone(true); $('ul').append($node); }); $('ul li:eq(2)').click(function(){ //可以使用this来访问符合$('selecotr')查询条件的节点 //alert(this.innerHTML); alert($(this).html()); }); $('#b1').click(function(){ $('#d1').attr({"class":"s1","style":"font-size:40pt"}); }); $('#b1').click(function(){ $('div:first').addClass('s1 s2'); $('div:first').removeClass('s2'); $('div:first').toggleClass('s1'); }); $('#b1').click(function(){ alert($('input[type=text]').val(); alert($('select').val()); //单选和多选框不能这样写 alert($(':radio').val()); alert($(':checkbox').val()); //要这样去写 var $node = $(':radio'); $node.each(function(){ //if($(this).attr('checked')){ // alert($(this).val()); //} if(this.checked){ alert(this.value); } }); }); $('#b1').click(function(){ var $items = $('ul').children(); //如果查询返回的是多个节点,可以使用length属性返回节点的个数 alert($items.length); //如何遍历 $items.each(function(i){ //$(this)html(); alert(this.innerHTML); }); }); }); </script> <style> .s1{ color:yellow; } .s2{ border:1px solid black; } </style> <body> <div>hello</div> <ul> <li>item1</li> <li id="i1">item2</li> <li>item3</li> </ul> <div id="d1" style="background-color:red;">hello</div> <input type="button" value="clickMe" id="b1"/> <input type="text" name="name"/><br/> male:<input type="radio" name="sex" value="m"/> female:<input type="radio" name="sex" value="f"/> fishing:<input type="checkbox" name="intrest" value="fishing"/> cookinng:<input type="checkbox" name="intrest" value="cooking"/> sleeping:<input type="checkbox" name="intrest" value="sleeping"/> <select> <option value="bj">beijing</option> <option value="sh">shanghai</option> <option value="tj">tianjing</option> </select> </body>
The above is Xiaoqiang's HTML5 mobile development road (36) - the content of DOM operations in jQuery. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!