Dmi*_*ris 1 html javascript css jquery
我是JavaScript的新手,只是我第一次尝试学习.作为一个小练习,我试着这么简单.
我正试图重新定位我在页面加载时的按钮.我做错了什么?
我将不胜感激.
我的代码如下:
<style type="text/css">
.mybutton{
position:absolute;
}
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
<script type="text/javascript">
window.onload=function(){
var mytestdiv = document.getElementById("testdiv");
var mytestbutton = document.getElementById("testdiv").childNodes()[0];
var y = mytestdiv.offsetWidth;
var x = mytestdiv.offsetHeight;
mytestbutton.style.right = x;
mytestbutton.style.top = y;
}
</script>
Run Code Online (Sandbox Code Playgroud)
而我非常简单的HTML:
<body>
<div id="testdiv" style="width:500px;border:solid #000000">
<input type="submit" id="myButton" value="TestMe">
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
编辑: 我在furebug中遇到的错误是:
mytestbutton.style is undefined
Run Code Online (Sandbox Code Playgroud)
使用CSS设置位置时,您需要指定使用的测量值...试试这个:
mytestbutton.style.right = x + "px";
mytestbutton.style.top = y + "px";
Run Code Online (Sandbox Code Playgroud)
编辑:
当它应该是"#mybutton"时,你也有".mybutton".是指类,#是指ID
最后.childNodes不是一个属性,因此你不需要(),加上你有空格,所以你需要删除它或使用.childNodes [1]而不是.childNodes [0].
var mytestbutton = document.getElementById("testdiv").childNodes[1];
Run Code Online (Sandbox Code Playgroud)
或者你可以去
var mytestbutton = document.getElementById("myButton");
Run Code Online (Sandbox Code Playgroud)