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