【发布时间】:2022-11-24 03:40:54
【问题描述】:
我正在尝试使用类在多个 div 中附加 HTML。但是我的代码在一个部分(HTML 1)中复制了 HTML,而另一个部分是正确的。
HTML 1:
<div class="about-author">
<div class="author-text">
This is a author bio. <a href="https://example.com">example</a><a href="https://example.com">example</a>
</div>
</div>
HTML 2:
<div class="author-info">
<div class="author-bio">
This is a author bio. <a href="https://example.com">example</a><a href="https://example.com">example</a>
</div>
</div>
JavaScript:
$(".about-author .author-text,.author-info .author-bio").each(function () {
var $this = $(this),
link = $this.find("a");
link.each(function () {
var $this = $(this),
cls = $this.text().trim(),
url = $this.attr("href");
$this.replaceWith('<li class="' + cls + '"><a href="' + url + '" title="' + cls + '" rel="noopener noreferrer" target="_blank"/></li>')
});
if (link.length) {
$this.parent().append('<ul class="author-links social social-color"></ul>');
}
$this.find("li").appendTo(".author-links")
});
我想要 HTML 1 和 HTML 2 中的相同输出
【问题讨论】:
标签: jquery