gpt4 book ai didi

javascript - 使用 jQuery 确定元素是否为文本节点

转载 作者:数据小太阳 更新时间:2023-10-29 04:56:40 26 4
gpt4 key购买 nike

给定一个 jQuery 元素,我如何确定右边的同级元素是文本节点而不是另一个元素?在 PHP 中,您会将 nodeType#text 进行比较 - 在​​这种情况下,什么是等价物?

window.jQuery('body').find('a').each(function (i) {


if(window.jQuery(this).next() == '?'){

}


});

我正在尝试弄清楚我可以在条件部分放入什么。

更新

    if(window.jQuery(this).next().length != 0){

alert(window.jQuery(this).next().get(0).nodeType);

if(window.jQuery(this).next().get(0).nodeType == 3){

alert('right has text');

}

出于某种原因,我的所有测试都返回 1 而不是 3 来指示文本节点!

最佳答案

next() 只返回元素,所以你不能用它来遍历文本节点。您可以改为使用 DOM nextSibling 属性并检查其 nodeType 属性:

现场演示:http://jsfiddle.net/kD9qs/

代码:

window.jQuery('body').find('a').each(function (i) {
var nextNode = this.nextSibling;
if (nextNode && nextNode.nodeType == 3) {
alert("Next sibling is a text node with text " + nextNode.data);
}
});

关于javascript - 使用 jQuery 确定元素是否为文本节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597943/

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