猿问

js怎么用nextElementSibling查找下下个节点

<script type="text/javascript">

    function getElements(){

    var x=document.getElementById("dialogArea");

    x.nextElementSibling.nextElementSibling.value="30";//该方法错误

    }

</script>


<input type="hidden" name="dinwei" class="dinwei" id="dialogArea" value="" />

<input name="myInput" type="text" size="20" value="How1 many input elements?"/>

<input name="myInput" type="text" size="20" value="How2 many input elements?"/>

<input name="myInput" type="text" size="20" value="How3 many input elements?"/>

<input type="button"  onclick="getElements()"value="button" />

1.用nextElementSibling查找id为dialogArea的下下个节点并修改它的值。
2.只用原生js不用jquery。
3.目的是为了在一些特殊情况下不能获取到input的ID以及class的情况下,用隐藏的input作为定位坐标向下查找指定input。

慕村225694
浏览 851回答 1
1回答

阿晨1998

然后你的html不能换行,换行了会每个input后面有个text类型的nextSibling,空白字符也相当于一个文本节点var x=document.getElementById("dialogArea");x.nextElementSibling.nextElementSibling.value = "30";<input type="hidden" name="dinwei" class="dinwei" id="dialogArea" value="" /><input name="myInput" type="text" size="20" value="How1 many input elements?"/><input name="myInput" type="text" size="20" value="How2 many input elements?"/><input name="myInput" type="text" size="20" value="How3 many input elements?"/><input type="button"&nbsp; onclick="getElements()"value="button" />最好用jquery,非要用js也可以这样写。var x=document.getElementById("dialogArea");next(next(x)).value="30";function next(e){&nbsp; &nbsp; &nbsp; &nbsp; e = e.nextSibling;&nbsp; &nbsp; &nbsp; &nbsp; if(e.nodeType == 3){ // 3是指text类型&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e = e.nextSibling;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return e;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答