首先申明js是放在外部的,在html的头部进行引用,在js中,"var step=searchTip.getElementsByTagName('div')"报错,求大神指教。错误提示如下:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
html结构如下:
<div class="searchTips">
<div><a>下一步</a><span title="关闭">关闭</div>
<div><a>下一步</a><span title="关闭">关闭</div>
<div><a>下一步</a><span title="关闭">关闭</div>
<div><a>下一步</a><span title="关闭">关闭</div>
<div><a>下一步</a><span title="关闭">关闭</div>
</div>
js代码如下
var searchTip=document.getElementByClassName('searchTips');
var step=searchTip.getElementsByTagName('div');
var next=step.getElementsByTagName('a');
var close=step.getElementsByTagName('span');
我找出来问题了,getElementByClassName()返回的是nodelist,因此在searchTip.getElementsByTagName('div')报错,应该这么写: