$("div").each(function (index, domEle) {
// domEle == this
$(domEle).css("backgroundColor", "yellow");
if ($(this).is("#stop")) {
$("span").text("Stopped at div index #" + index);
return false;
}
注意下面的each方法中我们使用了$.get()方法,在$.get()方法中不能使用this,所以alert(this.id)将输出undefined,需要再外面将this赋给一个变量,通过这个变量来操作。
$("#clothing").find("a").each(function(i){
var tn=$(this).text();
var self=this;
$.get("${pageContext.request.contextPath}/portal/addlink.action",{"tradeName":tn},function(data){
self.href="${pageContext.request.contextPath}/portal/product.action?tradeId="+data;
alert(this.id);
});
});
或者
var aes=$("#clothing").find("a");
aes.each(function(i){
...
});
.appendTo(expr),把所有匹配元素追加到另一个指定的元素expr中。这里的expr只能为string。例如:
<p>I would like to say: </p><div id="foo"></div>
$("p").appendTo("#foo");//将段落p追加到<div id="foo">中了。