$(function(){
$(".test").each(function(){
test();
});
});
function test(){
$(this).css("border","1px solid red").append(" checked");
}
为什么这不起作用?我缺少什么? 这是我的测试 html:
<p>test</p>
<p>test</p>
<p class="test">test</p>
<p>test</p>
最佳答案
$(this) 不能以这种方式使用,因为您没有指定 test() 函数的上下文:
$(".test").each(function(){
test($(this));
});
function test(el){
el.css("border","1px solid red").append(" checked");
}
或
$(".test").each(function(){
test.call(this);
});
function test(){
$(this).css("border","1px solid red").append(" checked");
}
关于jQuery 在each() 和自定义函数方面遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2176341/