gpt4 book ai didi

javascript - this.nextSibling 不工作

转载 作者:可可西里 更新时间:2023-11-01 01:49:20 26 4
gpt4 key购买 nike

我希望下面的代码能够提醒“out”

<input type=text onfocus="alert(this.nextSibling.id)" />
<output id="out">this is output</output>

但它会提示 undefined为什么?

最佳答案

nextSibling 选择元素的下一个兄弟节点。下一个节点也可以是 textNode,它没有 id 属性,因此您会得到 undefined 值。正如另一个答案所建议的那样,您可以使用 nextElementSibling 属性,该属性指的是 nodeType1 的下一个同级节点(即 Element 对象)或删除元素之间的隐藏字符。

请注意,IE8 不支持 nextElementSibling 属性。

关于javascript - this.nextSibling 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26945438/

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