gpt4 book ai didi

javascript - 查找元素内的所有@tagnames,然后在其上插入链接

转载 作者:行者123 更新时间:2023-11-30 11:14:12 26 4
gpt4 key购买 nike

我有我的html

<div class="comment-details">Hello @samplename This is my comment</div>
...
...
More .comment-details elements

在页面加载时,我想在 .comment-details 中找到这个 @samplename类使用 .each()循环并将其插入 <a href="samplelink"></a> jQueryJavascript

中的标记

页面加载后我想要这样的东西:

<div class="comment-details">Hello <a href="samplelink">@samplename</a> This is my comment</div>

最佳答案

jQuery 实现:

$('.comment-details').each(function(){
$(this).html($(this).html().replace(/\@.+?\b/g, `<a href=profile/${$(this).html().match(/\@.+?\b/g)[0].replace('@','')}>${$(this).html().match(/\@.+?\b/g)}</a>`));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="comment-details">Hello @samplename This is my comment</div>
<div class="comment-details">Hello @JG This is my comment</div>

enter image description here

关于javascript - 查找元素内的所有@tagnames,然后在其上插入链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52319421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com