javascript - 如何使用jquery获取元素的title属性并将其插入到元素后面?

标签 javascript jquery html attributes jquery-append

我有一个包含这样的列表元素的列表...

<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>

关于javascript - 如何使用jquery获取元素的title属性并将其插入到元素后面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40133680/

相关文章:

javascript - 如何在文本字段中输入内容时显示下拉框

javascript - 使用ajax提交时发送请求和参数

javascript - 自定义图表需要更多帮助

java - JSF SelectOneRadio 按钮无法禁用

javascript - jQuery每个数组索引附加到div

javascript - for循环数组长度

javascript - ColdFusion 中的 AJAX 请求

javascript - "WARNING - Suspicious code. The result of the ' getprop' operator is not being used."是什么意思?

css - 如何将滚动条更改为自定义设计? (避免使用默认浏览器外观)

javascript - 缩放嵌入内容以适合窗口? Flash 或 HTML5 视频