我很郁闷,不知道哪里错了
<script type="text/javascript" language="javascript">
var lw = document.getElementsByName("pa");
document.write(lw.length);
for(var i=0;i<lw.length;i++)
{
lw[i].style.marginTop = Math.ceil(Math.random()*(690-10)+10) + "px";
lw[i].style.marginLeft = Math.ceil(Math.random()*(690-10)+10) + "px";
}
</script>
</head>
<body>
<input name="pa" type="checkbox">
<input name="pa" type="checkbox">
<input name="pa" type="checkbox">
<div name="pa" class="par">
<div class="lw">
adsfasdfasdfasdfasdfasdfasdf
</div>
</div>
<div name="pa" class="par">
<div class="lw">
a;dslkfjalskjdfakjsdf;alskdfasdfasdfasdfasdf好像好像好像好像好像好像好像好像asdf
</div>
</div>
<div name="pa" class="par">
<div class="lw">
a;dslkfjalskjdfakjsdf;alskdfasdfasdfasdfasdf好像好像好像好像好像好像好像好像asdf
</div>
</div>
<div name="pa" class="par">
<div class="lw">
a;dslkfjalskjdfakjsdf;alskdfasdfasdfasdfasdf好像好像好像好像好像好像好像好像asdf
</div>
</div>
<div name="pa" class="par">
<div class="lw">
a;dslkfjalskjdfakjsdf;alskdfasdfasdfasdfasdf好像好像好像好像好像好像好像好像asdf
</div>
</div>
<div name="pa" class="par">
<div class="lw">
a;dslkfjalskjdfakjsdf;alskdfasdfasdfasdfasdf好像好像好像好像好像好像好像好像asdf
</div>
</div>
不知道哪里有问题,var lw = document.getElementsByName("pa") 文档里明明有那么多name=pa的,可lw的length是0,后面的代码也走不了,顺便请教一下,让name=pa的div随即位置 是这么写么
------解决方案--------------------先后顺序问题,你把它放到底部就应可以的,或写在函数里进行调用
------解决方案--------------------他执行的时候,后面的脚本还没有创建出html元素,你当然访问不到了
------解决方案--------------------