gpt4 book ai didi

javascript - 同时迭代和替换跨度

转载 作者:行者123 更新时间:2023-11-28 20:29:23 25 4
gpt4 key购买 nike

假设我有以下代码迭代 <span>里面<div> :

$('#recommendTextArea').children('span').each(function () {
//mentionedFriends.push($(this).html());
var mentionedFriendName = $(this).html();
jQuery.each(friendsList, function () {
if (this.name == mentionedFriendName) {
var facebookId = '@[' + this.id + ']';
}
});
});

我本质上想替换这个<span>字符串为 facebookId 。这样的事可能吗?如果是的话怎么办?

最佳答案

使用replaceWith

$('#recommendTextArea').children('span').each(function () {
var $span = $(this);

//mentionedFriends.push($(this).html());
var mentionedFriendName = $(this).html();
jQuery.each(friendsList, function () {
if (this.name == mentionedFriendName) {
var facebookId = '@[' + this.id + ']';
$span.replaceWith(facebookId);
}
});
});

编辑:如果您需要处理特殊字符等,您可以这样做:

$span.replaceWith($("<div>").text(facebookId).contents());

关于javascript - 同时迭代和替换跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16727111/

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