[英]jquery how to change clicked attribute
I have code like this: 我有这样的代码:
<div id="mtg_card"><a href="#" onclick="someEvent()"><img class="mtg_shop_cart" src="images/shop_cart_green.png" alt="" /></a></div>
it's going through loop so I have this same code X times... all is in one div called rounded-corner, and I wanted to change image in clicked div... I was trying code like this: 它正在经历循环,所以我有相同的代码X次...全部都在一个称为圆角的div中,并且我想在单击的div中更改图像...我正在尝试这样的代码:
<script>
$( window ).load(function() {
$('#rounded-corner #mtg_card').click(function(){
$(this).$(".mtg_shop_cart").attr("src","http://mgla.pl/images/shop_cart.png");
});
});
</script>
I would be greatful if you could help me with that. 如果您能帮助我,我将不胜感激。
<script>
$(function() {
$('#rounded-corner #mtg_card').click(function(){
$(this).find(".mtg_shop_cart").attr("src","http://mgla.pl/images/shop_cart.png");
return false;
});
});
</script>
This seems about right. 这似乎是正确的。 Just remember you can't have multiple elements with same ID in your HTML.
请记住,HTML中不能有多个具有相同ID的元素。
You need to return false in order for the event handlers to realize the event was received: 您需要返回false以便事件处理程序认识到事件已被接收:
<script>
$( window ).load(function() {
$('#rounded-corner #mtg_card').click(function(){
$(this).find(".mtg_shop_cart").attr("src","http://mgla.pl/images/shop_cart.png");
return false
});
});
</script>
Two problems: 两个问题:
You should replace the id="mtg_card" with a class, and then do this: 您应将id =“ mtg_card”替换为一个类,然后执行以下操作:
<div class="mtg_card"><a href="#" onclick="someEvent()"><img class="mtg_shop_cart" src="images/shop_cart_green.png" alt="" /></a></div>
<script>
$( window ).load(function() {
$('#rounded-corner .mtg_card').click(function(){
$(this).find(".mtg_shop_cart").attr("src","http://mgla.pl/images/shop_cart.png");
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.