jQuery子元素的更改属性 [英] JQuery change atributes of child elements
问题描述
我有这段代码,我想单击favoriteButton
类的div,要更改当前img
标记的当前(this
)<a>
和src
中的文本.
更改<a>
中的文本可以正常工作,但图像不起作用.
我尝试将另一个作为第一个子代,代码工作正常,仅在嵌套img
中不起作用
I have this code, and I want on click on div with favoriteButton
class, text in current (this
) <a>
and src
of current img
tag to be changed.
Changing text in <a>
work fine but image not work.
I try to put another as child of first and code work fine, only in nested img
not work
$('.favoriteButton').click(function(e){
$(this).find('.aclass').text("yeeee");
$(this).find('.imgclass').attr("src", "remove_favorit.png");
e.preventDefault();
});
<div class="favoriteButton" >
<a class="aclass" href="">Text
<img class="imgclass" src="add_heart.png">
</a>
</div>
<div class="favoriteButton" >
<a class="aclass" href="">Text
<img class="imgclass" src="add_heart.png">
</a>
</div>
有人可以帮我如何使用JQuery解决该问题吗?
Can some one help me how to solve that using JQuery
谢谢
推荐答案
这是因为图像位于链接内部,所以当您更改链接的.text()
值时,图像就会消失.要解决此问题,请将图像放在链接之外,或将其缓存并在更改文本后将其附加.
That's because the image is inside the link, so when you change the .text()
value of the link, the image disappears. To solve this, take the image outside of the link, or cache it and append it after you change the text.
这篇关于jQuery子元素的更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!