以下代码不起作用:
find('img').attr('src')
我希望将鼠标悬停在旁边区域并打开/关闭图像更改.
HTML
<aside style="width:200px;height:300px;text-align:center;">
<img class="img" src="imgs/photo_off.png" alt="" >
</aside>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
$("aside").hover(
function () {
$(this).find('img').attr('src') = $(this).find('img').attr('src').replace("_off", "_on");
},
function () {
$(this).find('img').attr('src') = $(this).find('img').attr('src').replace("_on", "_off");
}
);
Run Code Online (Sandbox Code Playgroud)
您不能像这样为函数调用语句赋值.它应该抛出一个错误
未捕获的ReferenceError:赋值中的左侧无效
$(this).find('img').attr('src', $(this).find('img').attr('src').replace("_off", "_on"));
Run Code Online (Sandbox Code Playgroud)
$("aside").hover(function () {
$(this).find('img').attr('src', function (i, src) {
return src.replace("_off", "_on");
})
}, function () {
$(this).find('img').attr('src', function (i, src) {
return src.replace("_on", "_off");
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17040 次 |
| 最近记录: |