簡體   English   中英

如何獲取元素的標題屬性並將其插入到元素后使用jQuery?

[英]How to get title attribute of element and insert it after element using jquery?

我有一個包含這樣的列表元素的列表...

<li class="first leaf">
  <a href="/admin/store/orders/view" title="View and process the orders.">View orders</a>
</li>

我想采用title屬性並將其作為div附加到<a>之后,就像這樣...

<li class="first leaf">
  <a href="/admin/store/orders/view" title="View and process the orders.">View orders</a>
  <div>View and process the orders.</div>
</li>

到目前為止,我在這里...

(function ($) {    
  $('#block-menu-menu-admin-welcome li a').each(function () {
    $(this).append('<div>' + $(this).attr('title') + '</div>');
  });
})(jQuery);

但這不起作用。 有人可以幫忙嗎?

jQuery .append()將html插入選定的元素,但是您需要在選定的元素之后插入html。 使用.after()代替。

$('#block-menu-menu-admin-welcome li a').each(function () {
    $(this).after('<div>' + $(this).attr('title') + '</div>');
});

 $('a').each(function(){ $(this).after('<div>' + this.title + '</div>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="first leaf"> <a href="/admin/store/orders/view" title="View and process the orders.">View orders</a> </li> </ul> 

您在循環中的第一個this是對錨的引用。 您必須將其梳理到其父元素,然后追加到其父元素。

$(this).parent().append('<div>' + $(this).attr('title') + '</div>');

這是給您的工作之地

$(function(){

     $('.first').append($('.first a').attr('title'));

});

暫無
暫無

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

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