this.nextSibling不工作 [英] this.nextSibling not working

查看:111
本文介绍了this.nextSibling不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以下代码提醒out

I expect the following code to alert "out"

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

但它提示未定义
为什么?

But it alerts undefined WHY?

推荐答案

nextSibling 选择元素的下一个兄弟节点。下一个节点也可以是 textNode ,它没有 id 属性,因此你得到 undefined 值。正如另一个答案所示,您可以使用 nextElementSibling 属性,该属性引用 nodeType <$的下一个兄弟节点c $ c> 1 (即一个Element对象)或删除元素之间的隐藏字符。

nextSibling selects the very next sibling node of the element. The very next node can also be a textNode which doesn't have an id property, hence you get the undefined value. As the other answer suggests you could use the nextElementSibling property which refers to the next sibling node that has nodeType of 1 (i.e. an Element object) or remove the hidden characters between the elements.

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

Note that IE8 doesn't support the nextElementSibling property.

这篇关于this.nextSibling不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆