jQuery each中的this
使用
$.each(function(){
alert(this.nodeName);
alert($(this));
});
要注意二者的不同this是一个dom对象,要使用jQuery的方法必须用jQuery(this)包含,负责只能用dom对象的方法,看下面的例子
<html> <head> <title>nth-child-test</title> <script src="jquery-1.6.1.min.js"></script> <script type="text/JavaScript"> $(function(){ $("tr").each(function(){ $(this.nodeName+":nth-child(2)").attr("style","color:red;"); }); }); </script> </head> <body> <table> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>2</td> <td>2</td> </tr> <tr> <td>3</td> <td>3</td> <td>3</td> </tr> </table> </body> </html>
<html> <head> <title>jQuery nth-child example</title> <script type="text/javascript" src="jquery-1.6.1.min.js"></script> </head> <body> <h1>jQuery nth-child example</h1> <table border=1> <tr><td>Row #1</td></tr> <tr><td>Row #2</td></tr> <tr><td>Row #3</td></tr> <tr><td>Row #4</td></tr> <tr><td>Row #5</td></tr> <tr><td>Row #6</td></tr> <tr><td>Row #7</td></tr> <tr><td>Row #8</td></tr> <tr><td>Row #9</td></tr> <tr><td>Row #10</td></tr> </table> <br/> <button>:nth-child(4)</button> <button>:nth-child(3n)</button> <button>:nth-child(even)</button> <button>:nth-child(odd)</button> <script type="text/javascript"> $("button").click(function () { var str = $(this).text(); $("tr").css("background", "white"); $("tr" + str).css("background", "coral"); }); </script> </body> </html>
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18