我有 3 个具有这种结构的类(这是我的网络应用程序中的 slider ):
<div class="emotion--digital-publishing">
<div class="dig-pub">
<div class="bg--image">/div>
<div class="dig-pub--layer center center">
<div class="layer--wrapper">
<div class="layer--content">
<div class="dig-pub--button">
<a href="/wilson-camo"></a>
</div>
</div>
</div>
</div>
</div>
</div>
我想获取 a
的 href
属性,并使用 this url 将 href
属性设置为 挖掘酒吧
。这对我来说非常重要,这是链接(我点击了哪个类),因为 3 个类有不同的链接。
我想使用 jQuery。
最佳答案
您将点击事件绑定(bind)到 anchor 标记。如果页面上有很多 anchor 标记,您还需要为 anchor 标记分配一个类,因此请将“className”替换为您的类名称。我不确定您想如何将其分配给 div,因此我将其作为数据属性完成,因为这是传统的方法。
$('a.className').on('click', function (){
$(this).closest('.dig-pub').attr('data-href', $(this).attr('href'));
});
关于javascript - 获取一个href属性并将该属性设置给其他div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006722/