gpt4 book ai didi

javascript - Jquery $(element).contents().first().text ("new text") 不起作用?

转载 作者:数据小太阳 更新时间:2023-10-29 05:36:09 24 4
gpt4 key购买 nike

我在 Google 或此处找不到任何相关信息。

我有一个 div,里面有一些文本和一些 html:

<div id="test-div">
http://<strong>somewebsite.com</strong>/big/long/unfriendly/path/
</div>

我想做的是添加一个 <wbr>在每个斜线之后。 (因为该值不会以其他方式换行并弄乱了我的表格)。在 $('#test-div').html() 上做一个简单的替换也会与强标签混淆,所以这不是一个选项。

我想使用 $('#test-div').contents()过滤掉文本部分(递归地)会起作用。但是我似乎无法编辑返回的各个位。我希望这会改变 http://部分:

$('#test-div').contents().first().text("something");

但是它什么都不做。我知道我的导航是正确的,因为像这样:

$('#test-div').contents().first().wrap( "<b></b>" );

确实有效。

为什么我不能更改文本位?(对于初始问题的更优雅的解决方案也很好)

最佳答案

像这样使用:

$('#test-div').contents().filter(function(){
return $.trim($(this).text()) == 'http://';
}).remove().end().prepend('something');

demo

或者,

$('#test-div').contents().first().remove().end().prepend("something");

demo

关于javascript - Jquery $(element).contents().first().text ("new text") 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22241391/

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