使用Javascript将两者结合起来<;a>;标签合一
我有这样的想法:使用Javascript将两者结合起来<;a>;标签合一,javascript,wordpress,Javascript,Wordpress,我有这样的想法: <a href="#"> ±</a> <a href="#"> Link Here</a> 我希望它看起来像: <a href="#"> ± Link Here</a> 但是,由于WordPress的局限性,我有点不确定如何在JavaScript方面将它们结合起来。这里: a1.textContent += a2.textContent; a2.par
<a href="#"> ±</a>
<a href="#"> Link Here</a>
我希望它看起来像:
<a href="#"> ± Link Here</a>
但是,由于WordPress的局限性,我有点不确定如何在JavaScript方面将它们结合起来。这里:
a1.textContent += a2.textContent;
a2.parentNode.removeChild(a2);
其中,a1
和a2
是对这两个锚元素的引用。(一旦获得对第一个锚点的引用,就可以像这样获得对第二个锚点的引用:a1.nextElementSibling
)
现场演示:此处:
a1.textContent += a2.textContent;
a2.parentNode.removeChild(a2);
其中,a1
和a2
是对这两个锚元素的引用。(一旦获得对第一个锚点的引用,就可以像这样获得对第二个锚点的引用:a1.nextElementSibling
)
现场演示:更新:请参阅@Šime Vidas的评论,了解更精简的方法
jQuery:
$('a + a').prev().html(function() {
$(this).html($(this).html() + $(this).next().html());
$(this).next().remove();
});
<a href="#"> ±</a>
<a href="#"> Link Here</a>
<a href="#"> ±</a>
<a href="#"> Another Link Here</a>
<a href="#"> ± Link Here</a>
<a href="#"> ± Another Link Here</a>
HTML:
$('a + a').prev().html(function() {
$(this).html($(this).html() + $(this).next().html());
$(this).next().remove();
});
<a href="#"> ±</a>
<a href="#"> Link Here</a>
<a href="#"> ±</a>
<a href="#"> Another Link Here</a>
<a href="#"> ± Link Here</a>
<a href="#"> ± Another Link Here</a>
输出:
$('a + a').prev().html(function() {
$(this).html($(this).html() + $(this).next().html());
$(this).next().remove();
});
<a href="#"> ±</a>
<a href="#"> Link Here</a>
<a href="#"> ±</a>
<a href="#"> Another Link Here</a>
<a href="#"> ± Link Here</a>
<a href="#"> ± Another Link Here</a>
更新:请参阅@Šime Vidas的评论,了解更精简的方法
jQuery:
$('a + a').prev().html(function() {
$(this).html($(this).html() + $(this).next().html());
$(this).next().remove();
});
<a href="#"> ±</a>
<a href="#"> Link Here</a>
<a href="#"> ±</a>
<a href="#"> Another Link Here</a>
<a href="#"> ± Link Here</a>
<a href="#"> ± Another Link Here</a>
HTML:
$('a + a').prev().html(function() {
$(this).html($(this).html() + $(this).next().html());
$(this).next().remove();
});
<a href="#"> ±</a>
<a href="#"> Link Here</a>
<a href="#"> ±</a>
<a href="#"> Another Link Here</a>
<a href="#"> ± Link Here</a>
<a href="#"> ± Another Link Here</a>
输出:
$('a + a').prev().html(function() {
$(this).html($(this).html() + $(this).next().html());
$(this).next().remove();
});
<a href="#"> ±</a>
<a href="#"> Link Here</a>
<a href="#"> ±</a>
<a href="#"> Another Link Here</a>
<a href="#"> ± Link Here</a>
<a href="#"> ± Another Link Here</a>
需要澄清,除非这是您想要对这个确切的标记所做的一切。使用wordpress
的限制?这两个锚是如何创建的?如果你解释了你是如何连接到不同的链接而不是一个,那么在需要澄清后,你可能会得到更好的答案,而不是将它们组合在一起,除非这是你真正想用这个确切的标记来做的。使用wordpress
的限制?这两个锚点是如何创建的?如果您解释如何访问不同的链接而不是一个,那么在您可以将这两行精简为以下内容后,您可能会得到更好的答案,而不是将它们组合在一起:$(this).append($(this.next().remove().html())代码>-@Šime:谢谢。我对jQuery相当陌生,所以我还不知道所有的快捷方式。:)您可以将这两行精简为:$(this.append($(this.next().remove().html())代码>-@Šime:谢谢。我对jQuery相当陌生,所以我还不知道所有的快捷方式。:)