这是我的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.
任何帮助?
答案 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/>