top,left,width,height 都是只读

解决方案 »

  1.   

    可以啊你函数是不是写错了??<html>
    <head><link type="text/css" rel="Stylesheet" href="" /></head>
    <body>
    </body>
    </html>
    <script>
    function createDiv(divId,top,left,height,width,font,color,size,text) 

            this.div = document.createElement("div");   //创建的div 
             this.div.id = divId; 
            this.div.style.top = top;    //设置style.top 等属性 
            this.div.style.left = left; 
            this.div.style.width = width; 
            this.div.style.height = height; 
            this.div.style.color = color; 
            this.div.style.fontFamily = font; 
            this.div.style.fontSize = size; 
            this.div.style.border="solid 1px black";//加个边框,这样看起来才清楚
            this.textNode = document.createTextNode(text); 
            this.div.appendChild(this.textNode); 
             //你函数这里写错了吧???????????
            this.changeWidth=function(newWidth)//修改style属性,都是用类似的方法修改的,就不把所以函数都写出来了 
            { 
                this.div.style.width = newWidth; 
                         
           } //你函数这里写错了吧???????????
            this.changeText=function(newText) 
            { 
                 this.textNode.nodeValue = newText; 
            } 
            this.changeFont = function(newFont) 

    this.div.style.fontFamily = newFont; 

    } var div1 = new createDiv("div1","100px","100px","500px","500px","Arial","blue","25px","text"); document.body.appendChild(div1.div); </script>然后在浏览器地址栏输入下面的就可以看到效果了javascript:div1.changeWidth("100px");void(0);
      

  2.   

    left,top不起作用可能是没有设置this.div.style.position="absolute造成的但width,和height不起作用还不清楚是不是css什么的造成的,我用下面的程序实验没问题
    <script>    
    function $(strId){
    return document.getElementById(strId);
    }function ChangeWidth(nWidth){
    var oDiv=$("div1");
    oDiv.style.width=nWidth+"px";
    }function ChangeHeight(nHeight){
    var oDiv=$("div1");
    oDiv.style.height=nHeight+"px";
    }function ChangeTop(nTop){
    var oDiv=$("div1");
    oDiv.style.top=nTop+"px";
    }function ChangeLeft(nLeft){
    var oDiv=$("div1");
    oDiv.style.left=nLeft+"px";
    }
    </script>
    <input type="button" value="Width" onClick="ChangeWidth('100')"/>
    <input type="button" value="Height" onClick="ChangeHeight('100')"/>
    <input type="button" value="Top" onClick="ChangeTop('100')"/>
    <input type="button" value="Left" onClick="ChangeLeft('100')"/>
    <div id="div1" style="border:1px solid #FF0000;width:500px;height:300px;position:absolute;"></div>
      

  3.   

    恩,谢谢大家,解决了。。其实是width,height是改变了的,像showbo说的,加了边框就看出来了。。top,left值有问题,xingqiliudehuanghun 说的设置了style.position就好了。。谢啦~~~