Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:将变量设置为nodeValue无效_Javascript_Variables - Fatal编程技术网

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;