如何将 var this 放入图片中的attr? 因为那被替换为innerHTML
<!DOCTYPE html>
<html>
<head><script type="text/javascript" src="http://line25.com/wp-content/uploads/2010/jquery-lightbox/demo/js/jquery.js"></script>
</head>
<body>
<button href="http://allcartooncharacters.com/wp-content/uploads/2014/05/Tweety-310x310.png">Try it</button>
<p id="demo">
<img src="http://www.w3schools.com/jquery/img_pulpitrock.jpg" alt="Pulpit Rock" width="284" height="213">
</p>
<script>
$(document).ready(function() {
$('button').click(function() {
var this = $("button")[0].getAttribute("href");
$("#demo img").attr("src", '+ var this +');
});
});
</script>
</body>
</html>
如果我想添加加载功能怎么办?也许如果图像成功打开,加载将像这样消失
if (loading) {
$(".loading-picture-gif").show();
}
else {
$(".loading-picture-gif").hide();
}
答案 0 :(得分:1)
您不能将error[E0507]: cannot move out of borrowed content
--> src/main.rs:14:13
|
14 | bar(*a);
| ^^ cannot move out of borrowed content
用作变量名称,即使可以,也可能不是一个好主意。我的代码如下所示
this
对于加载图像的隐藏/显示,当您将图像的$("button").click(function() {
var newSrc = $("button")[0].getAttribute("href");
$("#demo img").attr("src", newSrc);
});
属性更改为回调时,可以使用.on("load"...
。最终代码如下所示
src
答案 1 :(得分:1)
this
是一个保留关键字,你不应该将它用作变量名,而是你可以做的是
$(document).ready(function() {
$('button').click(function() {
var href = $(this).attr("href");
$("#demo img").attr("src", href);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button href="http://allcartooncharacters.com/wp-content/uploads/2014/05/Tweety-310x310.png">Try it</button>
<p id="demo">
<img src="http://www.w3schools.com/jquery/img_pulpitrock.jpg" alt="Pulpit Rock" width="284" height="213">
</p>
&#13;