[英]How can I set an attributes (base on child attributes) in jQuery or JavaScript?
I want to set an attribute on hover, based on his child attributes, by using jQuery. 我想使用jQuery根据其子属性设置悬停属性。 I have tried many ways to make this, but no luck.
我尝试了很多方法来实现这一目标,但是没有运气。 All attributes are same and not changing on hover.
所有属性都相同,并且悬停时不会更改。
<li class="menu-item">
<a data-src="" class="menu-item-link" href="#">Item one</a>
<span class="description">
<img src="https://i.ibb.co/LxSKFgH/image1.jpg">
</span>
</li>
<li class="menu-item">
<a data-src="" class="menu-item-link" href="#">Item two</a>
<span class="description">
<img src="https://i.ibb.co/TLJL7hq/image2.jpg">
</span>
</li>
What I want is when I hover on .menu-item
I want to set .description
's img attribute on data-src
attribute for each menu. 我想要的是当我将鼠标悬停在
.menu-item
我想在每个菜单的data-src
属性上设置.description
的img属性。 That means Attribute must set on his parent attribute. 这意味着必须在其父属性上设置Attribute。
You can achieve what you want by executing the following jQuery script: 您可以通过执行以下jQuery脚本来实现所需的功能:
$(".menu-item").hover(function(){
//Take the src value from the inner img tag
let srcValue = ($(this).find("img").attr("src"));
//Set the data-src attribute of the inner anchor tag
$(this).find("a").attr("data-src", srcValue);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.