以下代码为什么不弹出alert框?
我是想用一个alert来弹出<p>中的文本。
<html>
<head><script type="text/javascript">
var e = document.getElementById("pp");
var str=e.firstChild.nodeValue
alert(str);
</script></head><body><p id="pp" title="pTitle" >ABCDEFG</p></body></html>
我是想用一个alert来弹出<p>中的文本。
<html>
<head><script type="text/javascript">
var e = document.getElementById("pp");
var str=e.firstChild.nodeValue
alert(str);
</script></head><body><p id="pp" title="pTitle" >ABCDEFG</p></body></html>
<html>
<head><script type="text/javascript">
window.onload=function()
{
var e = document.getElementById("pp");
var str=e.firstChild.nodeValue
alert(str);
}
</script></head><body><p id="pp" title="pTitle" >ABCDEFG</p></body></html>你这样写试试。js要触发才能alert的!
我是在练习《Javascript DOM 编程艺术》上的实例时遇到这样的问题难道是书上错了?
书上的代码没有通过onload函数来触发
如果你把p标签写在js的前面,那么你的代码就运行正常了。<p id="pp" title="pTitle" >ABCDEFG</p>
<script type="text/javascript">
var e = document.getElementById("pp");
var str=e.firstChild.nodeValue
alert(str);
</script>