Javascript:将变量设置为nodeValue无效
设置变量时遇到问题,无法找到任何有用的文档 这项工作:Javascript:将变量设置为nodeValue无效,javascript,variables,Javascript,Variables,设置变量时遇到问题,无法找到任何有用的文档 这项工作: <!DOCTYPE html> <html> <body onload="alert(document.getElementById('foo').firstChild.nodeValue)"> <a id="foo" href="old">Foobar</a> </body> </html> 但这不起作用: <!DOCTYPE html
<!DOCTYPE html>
<html>
<body onload="alert(document.getElementById('foo').firstChild.nodeValue)">
<a id="foo" href="old">Foobar</a>
</body>
</html>
但这不起作用:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var theText = document.getElementById('foo').firstChild.nodeValue ;
</script>
</head>
<body onload="alert(theText)">
<a id="foo" href="old">Foobar</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var theElement = document.getElementById('foo') ;
var theText = theElement.firstChild.nodeValue ;
document.write(theElement.getAttribute('href')) ;
theElement.setAttribute('href', theText) ;
document.write(meta.getAttribute('href')) ;
</script>
</head>
<body>
<a id="foo" href="old">Foobar</a>
</body>
</html>
var theText=document.getElementById('foo').firstChild.nodeValue;
警报显示“未定义”。我真正想做的是这样的事情,这也不起作用:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var theText = document.getElementById('foo').firstChild.nodeValue ;
</script>
</head>
<body onload="alert(theText)">
<a id="foo" href="old">Foobar</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var theElement = document.getElementById('foo') ;
var theText = theElement.firstChild.nodeValue ;
document.write(theElement.getAttribute('href')) ;
theElement.setAttribute('href', theText) ;
document.write(meta.getAttribute('href')) ;
</script>
</head>
<body>
<a id="foo" href="old">Foobar</a>
</body>
</html>
var theElement=document.getElementById('foo');
var theText=theElement.firstChild.nodeValue;
document.write(theElement.getAttribute('href');
theElement.setAttribute('href',theText);
document.write(meta.getAttribute('href');
为什么不起作用?当脚本运行时,
foo
元素不存在。如果检查JavaScript控制台,您应该会看到一个错误,如下所示:
未捕获的TypeError:无法读取null的属性“firstChild”
出现此错误是因为如果未找到要查找的元素,则getElementById
将返回null
您需要在标记之后执行JavaScript代码。将脚本
标记移动到正文的底部
,或将其放入DOM就绪/加载事件处理程序中。例如:
<body onload="alert(theText)">
<a id="foo" href="old">Foobar</a>
<!-- Now the `foo` element actually exists, our script can find it -->
<script type="text/javascript">
var theText = document.getElementById('foo').firstChild.nodeValue;
</script>
</body>
var theText=document.getElementById('foo').firstChild.nodeValue;
脚本运行时,foo
元素不存在。如果检查JavaScript控制台,您应该会看到一个错误,如下所示:
未捕获的TypeError:无法读取null的属性“firstChild”
出现此错误是因为如果未找到要查找的元素,则getElementById
将返回null
您需要在标记之后执行JavaScript代码。将脚本
标记移动到正文的底部
,或将其放入DOM就绪/加载事件处理程序中。例如:
<body onload="alert(theText)">
<a id="foo" href="old">Foobar</a>
<!-- Now the `foo` element actually exists, our script can find it -->
<script type="text/javascript">
var theText = document.getElementById('foo').firstChild.nodeValue;
</script>
</body>
var theText=document.getElementById('foo').firstChild.nodeValue;