我在<span>里面<div>,像这样:
<div id="#mydiv">
<a href="#">Hello</a><span class="myImageArea"></span>
<div class="someClass">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用以下CSS:
.myImageArea {
background: url("../img/upArrow.png") no-repeat right 5px;
display: inline-block;
cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
我想改变background:url类的属性myImageArea
$('#mydiv').click(function() {
//change the background image of the class
});
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
在click函数中,您可以使用jQuery的选择器获得对您单击的元素的引用,现在可以在其中搜索该范围.一旦你有了,你就可以改变CSS属性'background-image':
$('#myDiv').click(function() {
$('.myImageArea', this).css('background-image', 'newimagepath.png');
});
Run Code Online (Sandbox Code Playgroud)