关于javascript的getelementbyid得到的为什么为null
<html>
<head>
<style type="text/css">
#temp{
font-size:150px;
color:red;
}
</style>
</head>
<body>
<script type="text/javascript">
var x= new Array();
x = document.getElementById('temp');
alert(x);
</script>
<div id="temp">123456</div>
</body>
</html>
把你的JS语句放到你要执行的检查的语句后面如:
<html>
<head>
<style type="text/css">
#temp{
font-size:150px;
color:red;
}
</style>
</head>
<body>
<div id="temp">123456</div>
<script type="text/javascript">
var x= new Array();
x= document.getElementById("temp");
alert(x);
</script>
</body>
</html>
这样写才可以.....
其实你这个语句是有问题的,我想你要得到的是DIV里面的值吧,这样是得不到值的,这样只是把DIV这个对象给了X。所以最后哪怕调式成功也只是显示OBJECT。
如果想得到它里面的内容,加上innerHTML或者innerText,就可以了。
<html>
<head>
<style type="text/css">
#temp{
font-size:150px;
color:red;
}
</style>
</head>
<body>
<div id="temp">123456</div>
<script type="text/javascript">
var x= new Array();
x= document.getElementById("temp").innerText;
alert(x);
</script>
</body>
</html>