这段代码取不到值

Zi_Yuan Zi_Yuan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请教大家,我这段代码哪里有问题?为什么只能执行setpreBg函数,缺不能执行setnextBg函数?为什么第一个alert能取到值,第二个却取不到?

function setBg(obj){
var preNode=obj.parentNode.previousSibling; //tr
var nextNode=obj.parentNode.nextSibling;
//alert(nextNode.nodeName);
obj.style.backgroundImage="url(images/btnbg.gif)";
if(preNode.nodeName=="TR")
setpreBg(preNode);
//alert(nextNode.nodeName);
if(nextNode.nodeName=="TR")
setnextBg(nextNode);

}
function setpreBg(pre){
pre.childNodes[0].style.backgroundImage="";
var preNode=pre.previousSibling; //td
if(preNode.nodeName=="TR")
setpreBg(preNode);
}
function setnextBg(next){
next.childNodes[0].style.backgroundImage="";
var nextNode=next.nextSibling;
if(nextNode.nodeName=="TR")
setnextBg(nextNode);
}


<table width="103" border="0"cellspacing="1" cellpadding="0" style="margin-left:22px; margin-top:24px; font-weight: bold; font-family: '黑体'; font-size: 12px;">
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小马</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小明</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小李</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小张</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小兰</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小新</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小吴</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小环</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小小</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小青</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小白</td>
</tr>
<tr>
<td width="101" height="24" background="" class="btnbj" style="text-align: right" onclick="javascript:setBg(this);">小黑</td>
</tr>
</table>
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
Zi_Yuan
JavaScript 87,983 成员 224,680 内容