【发布时间】:2021-04-16 11:00:56
【问题描述】:
我将捕获链接标题并将其写在<a href 链接之后,并将其写入span。将链接标记为段落并带有小图标的效果很好(见下图)。
See Example and hover the Icons next to the text
目前没有问题:
$( "a.sidenote-icon" ).wrapAll( "<span class='sidenote'><cite class='icon quelle'></cite></span>" );
var title = $( "a.sidenote-icon" ).attr('title');
var titlewrap = "<span>" + title + "</span>";
$( "cite" ).after(titlewrap);
如果我在几种情况下使用 each-function jQuery 将标题添加到一个跨度中,但是三倍 - 因为在我的段落中有三个链接。这是我的每个功能..
$( "a.sidenote-icon" ).each(function() {
$( this ).wrapAll( "<span class='sidenote'><cite class='icon quelle'></cite></span>" );
var title = $( this ).attr('title');
var titlewrap = "<span class='testy'>" + title + "</span>";
$( "cite" ).after(titlewrap);
});
【问题讨论】:
-
请提供正确的minimal reproducible example 问题。
-
$( "cite" )每次选择 allcite元素,您需要将其限制为正确的元素。 -
在那个地方试试
$( this ).parent().after(titlewrap);。 -
与 next() 或某事。像那样? Ahhh ok 从
this开始并遍历到父级。谢谢!
标签: javascript html jquery each