我有一个包含这样的列表元素的列表...
<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/