gpt4 book ai didi

javascript - nodeValue 和 indexOf 在我的情况下不起作用

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

http://jsfiddle.net/b1ekda2j/1/

我的html

<dd>
<br>作詞:汪峰
<br>作曲:汪峰
<br>
<br>他們將你狠狠地遺棄在街口
<br>流血的腦袋出現混亂的幻覺
<br>你是否在懷疑你拼死的付出
<br>是否再一次質疑堅守的信仰

我的 js

$(document).ready(function () {
$('br').each(function () {
var nodeValue = this.nextSibling.nodeValue;
console.log(nodeValue);
if (nodeValue.indexOf(':') > -1) {
$(this.nextSibling).remove();
$(this).remove();
}
});
});

我想删除 <br>包含 : 的符号但它不能通过使用 indexOf 工作,为什么?我控制 nodeValue,它确实选择了正确的 DOM。

最佳答案

如果仔细查看字符串中的符号 :,您会发现它不是字符 :(冒号)。要检查它,只需从字符串中复制它并使用 charCodeAt 方法,如下所示:

':'.charCodeAt(0)

它会给你 65306,这是“FULLWIDTH COLON” - http://www.fileformat.info/info/unicode/char/ff1a/index.htm

要搜索它,只需将代码中的冒号替换为这个:

$(document).ready(function () {
$('br').each(function () {
var nodeValue = this.nextSibling.nodeValue;
if (nodeValue.indexOf(':') > -1) {
$(this.nextSibling).remove();
$(this).remove();
}
});
});

但最好使用数字形式和注释:

nodeValue.indexOf('\uFF1A') // FULLWIDTH COLON

关于javascript - nodeValue 和 indexOf 在我的情况下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25829155/

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