jquery - 如何在 "href attribute"之后添加 "</a>"

标签 jquery insertafter

我想在结束标记 </a> 处显示“href”属性的值对于每个 anchor 标记。

相反,此代码仅适用于页面上的第一个链接。

jQuery.noConflict(); 
jQuery(document).ready(function($) {
    $('a').after($('a').attr('href'));
})(jQuery);

最佳答案

您需要以不同的方式调用该函数:

.after( function-html )

Type: Function( Integer index, String html ) => htmlString or Element or Text or jQuery A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert after each element in the set of matched elements. Receives the index position of the element in the set and the old HTML value of the element as arguments. Within the function, this refers to the current element in the set.

通过这种方式,对于每个 anchor ,您可以将要打印的正确文本返回到 after 方法:

$('a').after(function(idx, txt) {
    return this.getAttribute('href');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="url1">link text</a>
<a href="url2">link text</a>
<a href="url3">link text</a>
<a href="url4">link text</a>

关于jquery - 如何在 "href attribute"之后添加 "</a>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51900749/

相关文章:

php - Jquery 查找 div 是否到达滚动底部

javascript - 使用 jquery 删除行时,第 nth-child css 不会重绘

javascript - jQuery insertAfter 添加 2 个相同的元素

JQuery .insertAfter 在此示例中不起作用 - 为什么?

jquery - 以表格尺寸动态和响应地居中加载器图像

javascript - 将 JQuery ReplaceWith 与 JavaScript 变量结合使用

javascript - 使用 JQuery 在 HTML div 之后添加 CSS 类

javascript - 在 html 文本后附加 img

jquery - 在jquery中,如何显示ajax进度图像而不将控件向右移动

javascript - 隐藏 div 也会隐藏父 div 的图像