如何制作一个跨度,通过单击链接将其上划线,以便链接位于跨度的右侧?
我需要在跨度的右侧创建一个链接,并通过单击该链接使该跨度加盖上划线。
主要问题是链接和跨度都超出了范围,我不知道如何解决它。另一个问题是它们不能通过宽度来证明,因为它们看起来是在一起的。
我尝试将 insertAdjacentElement 的 beforeend 属性更改为 afterend,但它使链接出现在每个跨度下,这实际上不是我想要的。
提前致谢!
let p = document.querySelectorAll('p');
p.forEach(node => {
node.innerHTML = `<span>${node.textContent}</span>`;
})
for (let elem of p) {
let a = document.createElement('a');
a.href = '';
a.innerHTML = 'cross';
a.addEventListener('click', function(event) {
event.preventDefault();
elem.classList.toggle('overlined');
})
elem.insertAdjacentElement('beforeend', a);
}
I need to make a link to the right of a span, and make that span overlined by clicking the link.
The main problem is that both link and span overlines, and I don't know how to fix it. The other problem is they can't be justified by width, as they appear to be together.
I tried to change beforeend attribute of insertAdjacentElement to afterend, but it makes link appear under every span, which is actually not what I want.
Thanks in advance!
let p = document.querySelectorAll('p');
p.forEach(node => {
node.innerHTML = `<span>${node.textContent}</span>`;
})
for (let elem of p) {
let a = document.createElement('a');
a.href = '';
a.innerHTML = 'cross';
a.addEventListener('click', function(event) {
event.preventDefault();
elem.classList.toggle('overlined');
})
elem.insertAdjacentElement('beforeend', a);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您将
overlined
类应用到p
元素,该元素也包含您的cross
链接。使用p
的children[0]
或firstChild
将其应用到span
。我还建议向您的
span
和a
标记添加一些 css 类,以便您可以根据需要设置它们的样式。That's becasue you are applying the
overlined
class to thep
element which also contains yourcross
link. Apply it to thespan
by usingchildren[0]
orfirstChild
ofp
.I also suggest adding some css classes to your
span
anda
tags so you can style them as you wish.