获取输入javascript的下一个img元素

时间:2015-04-19 13:47:38

标签: javascript

这是我的HTML

<td>
    <input type="text" name="FullName"/> 
      <img/>
</td>

我试图按以下方式获取图像元素。但是,它不起作用 -

 var form = document.forms["myform"];
 var fullNameTextBox = form["FullName"];

 var thisImage = fullNameTextBox.nextSibling;
 thisImage.style.display = 'block'; //shows uncaught type error,can not set property display of undefined.

任何帮助?

2 个答案:

答案 0 :(得分:3)

您已使用nextElementSibling

var form = document.forms["myform"];
var fullNameTextBox = form["FullName"];
var thisImage = fullNameTextBox.nextElementSibling;
thisImage.style.display = 'block';

答案 1 :(得分:2)

.nextSibling会匹配文字节点(就像空白一样)

从html中删除空白

<input type="text" name="FullName"/><img/>

或使用.nextElementSibling