簡體   English   中英

jQuery屬性值marge

[英]Jquery Attribute value marge

我正在嘗試從span的屬性向錨的href添加一個屬性。 它覆蓋了現有的href即callto:值。 我希望它像這樣<a class="phone" href="callto:000-555-6666"></a>附加。 我怎樣才能做到這一點? 謝謝!

HTML

<a class="phone" href="callto:"></a>
<span class="path" phone="000-555-6666">Test</span>

JQuery的

jQuery(".path").mousemove(function(e) {
  jQuery(".phone").attr('href', jQuery(this).attr('phone')).attr(jQuery(this).attr('phone'));
});

您正在替換現有價值。 使用電話屬性值添加“呼叫到:”,然后設置href屬性。

 jQuery(".path").mousemove(function(e) { jQuery(".phone").attr('href',"callto:"+ jQuery(this).attr('phone')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="phone" href="callto:"></a> <span class="path" phone="000-555-6666">Test</span> 

我不確定您為什么在這里使事情變得過於復雜。 只需獲取屬性值,然后使用attr函數進行設置即可:

 jQuery(".path").mousemove(function(e) { var phone = $(this).attr('phone'); jQuery(".phone").attr('href', 'callto:' + phone); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="phone" href="callto:">test</a> <span class="path" phone="000-555-6666">Test</span> 

 jQuery(document).ready(function(){ jQuery(".path").mousemove(function(e) { var phone = jQuery(this).attr('phone'); jQuery(".phone").attr('href','callto:'+phone); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a class="phone" href="callto:">Phone Number</a> <span class="path" phone="000-555-6666">Test</span> 

在問題中,標簽沒有任何文本,因此您必須添加它。 從js方面,您必須獲取span的當前attr值並將其添加到代碼中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM