gpt4 book ai didi

javascript - document.getElementById 的问题

转载 作者:行者123 更新时间:2023-11-30 18:54:37 26 4
gpt4 key购买 nike

这有效:


alert(document.getElementById("Container").nodeName);

但这不是:


var CurParent = document.getElementById("Container");
alert(CurParent.nodeName);

我正在使用 IE7。为什么 ?

最佳答案

从您的最新评论来看,这似乎是变量作用域的问题。您确定 var parent真的全局的吗?由于不正确的变量范围,以下将工作:

function firstThing() {
var parent = document.body;
}

function secondThing() {
return parent;
}

firstThing();
secondThing(); // will return undefined

在您打算使用它的最大范围内定义一个变量。以下起作用。

var parent;

function firstThing() {
parent = document.body;
}

function secondThing() {
return parent;
}

firstThing();
secondThing(); // will return document.body

关于javascript - document.getElementById 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2568827/

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