<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script type="text/javascript">
function moveTo(obj,xL,yL) {
        obj.left = xL
        obj.top = yL
}
</script></head><body> 
<input id="11" name="text" value="test" /><script type="text/javascript">
var text = document.getElementById("11");
alert(text.value);
</script> <button id="22" name="button"  onclick="moveTo(text,233,44)"></body></html>
刚学js帮我看看这个,为什么不能得到我想要的效果?
我想做:
  定义个text,一个button
  当button 点击事件触发时,调用moveTo()把text的坐标x,y移动到233,44。为什么出不来??
初学,大家除了帮我看看除了哪里代码有问题外,我为什么会犯这样的错,到底是哪里有欠缺???

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><script type="text/javascript">
    function moveTo(obj,xL,yL){
        obj.style.left = xL
        obj.style.top = yL
    }
    var text;
    window.onload=function(){
    text = document.getElementById("a11");
    }
    </script>
    <body>
    <input id="a11" name="text" value="test" style="position:relative;"/>
    <button id="22" name="button"  onclick="moveTo(text,233,44)">xxxx</button>
    </body></html>
      

  2.   

    给想移动的对象加上position:absolute
      

  3.   

    你的代码少了很多东西啊!
    首先必须有obj.style.position="absolute";
    不然的话元素是不会移动的。
    其次最好加上单位。
    完整代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><script type="text/javascript">
    function moveTo(obj,xL,yL){
        obj.style.position="absolute";
        obj.style.left = xL+"px";
        obj.style.top = yL+"px";
    }
    var text;
    window.onload=function(){
        text = document.getElementById("a11");
    }
    </script>
    <body>
    <input id="a11" name="text" value="test" style="position:relative;"/>
    <button id="22" name="button"  onclick="moveTo(text,233,44)">xxxx</button>
    </body></html>
      

  4.   

    忘了说一点:元素的大部分属性都在style对象里
    应该是 obj.style.left 而不是 obj.left 
      

  5.   

    学习了,
      再追问一下3楼,
      JS操作HTML的对象,这些对象能像java直接用this吗??
      如 :
      <input id="a11" name="text" value="test" style="position:relative;"onchange="moveTo(this,233,44)"/>
      当text改变时,直接变位置,那么我就不需要再定义个button对象,另外操作来move text这个对象了??
      不知道这样是否可行,?
      另外,JS中的Object对象除了自定义的内置对象之外,要引用如HTML的对象,应该怎么做,注意些什么?  最后,你改的代码有一点没有看懂,如  obj.style.top = yL+"px";
      "px" 什么意思啊??  
      

  6.   

    HTML,CSS是基础,要狠下一番功夫的
      

  7.   

    楼上几位的给你说得很清楚了。style属性,再就是位置的设置。
      

  8.   

    好好学习一下CSS吧!少用js来做效果![e04]顶啊!
    周杰伦2010新专辑泄漏了!!!周杰伦2010新专辑MP3下载!
    http://www.zhoujielun2010.info/
      

  9.   

    w3c school
    去全部看一遍么