[英]get a href attribute and set this attribute to other div
我有3個具有這種結構的類(這是我的Web應用程序中的滑塊):
<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>
我想要得到href
屬性a
和設置href
屬性附加傷害這個網址來dig-pub
。 對我來說非常重要,因為這是鏈接(我單擊了哪個類),因為3個類具有不同的鏈接。 我想使用jQuery。
您將click事件綁定到錨標記。 如果頁面上有很多標簽,則也需要為錨標記分配一個類,因此將“ className”替換為您的類名稱。 我不確定您要如何將其分配給div,所以我將其作為數據屬性來完成,因為這是常規方法。
$('a.className').on('click', function (){
$(this).closest('.dig-pub').attr('data-href', $(this).attr('href'));
});
首先,向鏈接和div添加一個id(便於獲取元素)
<div id="dig-pub" class="dig-pub">
<a id="id" href="/wilson-camo"></a>
然后,獲取href
var href = $('#id').attr('href');
將值設置為div
$('#dig-pub').html(href);
但是,您可以在JQuery文檔中輕松找到它。
(別忘了在代碼段中關閉第3行的div)
jQuery('.dig-pub').on('click', function() {
url = jQuery(this).parent().find('a').attr('href');
jQuery(location).attr(url);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.