我有以下代码:
<div id="wrapper">
<div style="width:500px;background-color:yellow;"> // this is a parent div
<div style="width:260px;">
<a href="javascript:XXXXX">Click me to color only the FIRST yellow div</a>
</div>
</div>
<div style="width:500px;background-color:yellow;"> // this is a parent div
<div style="width:260px;">
<a href="javascript:XXXXX">Click me to color only the SECOND yellow div</a>
</div>
</div>
</div>
我可以使用jQuery更改父div的颜色而不提供其ID或名称吗?
由于
答案 0 :(得分:7)
$("a").click(function(e){
e.preventDefault();
$(this).parent().parent().css("color", "green");
});
答案 1 :(得分:4)
试试这个:
$('a').click(function() {
$(this).parent().parent().css('backgroundColor', 'yellow');
return false;
});