gpt4 book ai didi

javascript - 在 DOM 中向下和横向遍历

转载 作者:行者123 更新时间:2023-11-28 11:34:36 25 4
gpt4 key购买 nike

我有以下结构:

<a href="#" onclick="toggleThis(this); return false;">
<div style="width:100%;">
<h3 style="display:inline-block"><img src="/assets/images/plus_qty.png" class="faqPlusMinus">Where is my order?</h3>
</div>
</a>

我试图提醒图像标记的 src 但收到以下错误:

TypeError: e.firstChild.firstChild is null

代码如下:

function toggleThis(e){
var elc = e.firstChild.firstChild.nextSibling;
alert(elc.src);/
}

我没有使用 JQuery。

最佳答案

检查.firstChild的类型,它可能是与标签之间的空白对应的TextNode,而不是div标签。 TextNode 没有 firstChild

你说你没有使用 jQuery,但也许你可以使用 element.querySelector 。这将使您能够以这样的方式编写函数,即使有人在 HTML 中添加或删除某些节点,该函数仍然可以工作。

关于javascript - 在 DOM 中向下和横向遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26968779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com