gpt4 book ai didi

javascript - 去掉字符串中间的一个字符 : without removing inner element

转载 作者:太空狗 更新时间:2023-10-29 13:26:39 25 4
gpt4 key购买 nike

这个让我难住了。我想从 label 元素中删除“+”。这是 HTML:

 <label class="option" for="edit-attributes-21-33">
<input type="radio" id="edit-attributes-21-33" name="attributes[21]"
value="33" checked="checked" class="form-radio"> 4 oz, +$15.00</label>

我从这个开始

$(".option").each(function(index, value) {

$(this).text( $(this).text().replace("+", ""));

})

这会删除“+”,但也会删除输入元素。于是我尝试了:

$(".option").each(function(index, value) {

var oldString = $(this).html();
var newString = oldString.replace("+", "");
console.log(oldString, newString);
$(this).text(newString);

})

这会生成一个正确的 html 标记字符串,但它是一个字符串并以这种方式传递回 DOM。我看到另一个帖子有同样的问题,但没有解决方案。

最佳答案

您可以通过使用 .html() 来实现您想要的代码。而不是 .text() :

$(".option").each(function(index, value) {
var oldString = $(this).html();
var newString = oldString.replace("+", "");
console.log(oldString, newString);
$(this).html(newString);
});

这是 JQuery .html()方法引用:https://api.jquery.com/html/

这是 fiddle :https://jsfiddle.net/Darkseal/1c572Luw/

我也稍微修改了你的<input>结束标记以使其符合 XHTML。

关于javascript - 去掉字符串中间的一个字符 : without removing inner element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659144/

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