Javascript 如何将对象投射到DomeElement?

Javascript 如何将对象投射到DomeElement?,javascript,object,dom,casting,element,Javascript,Object,Dom,Casting,Element,我想将un对象强制转换为DOM对象 例如,我有一个构造函数: function Input(type, id, name, value) { this = document.createElement("input"); ... } 如您所见,我将bodythis=document.createElement(“输入”)用于尝试将输入对象强制转换为DomeElement对象,但它不起作用 (我知道我可以做this.input=document.createElement(“in

我想将un对象强制转换为DOM对象

例如,我有一个构造函数:

function Input(type, id, name, value)
{
    this = document.createElement("input");
    ...
}
如您所见,我将body
this=document.createElement(“输入”)用于尝试将输入对象强制转换为DomeElement对象,但它不起作用

(我知道我可以做
this.input=document.createElement(“input”);
,但我绝对想把唯一的
this
转换成
document.createElement(“input”);

你有什么想法吗


提前谢谢你,诚挚地

看看这个怎么样,它可能会给你一个想法,但如何去做呢


我不确定您想做什么,但您可以尝试以下方法:

<div id="container"></div>

<script type="text/javascript">

    var Input = function(type, id, name, value){
        this.element = this.create();

        this.setType(type);
        this.setValue(value);
        this.setName(name);

        return this.element;
    };

    Input.prototype.create = function(){
        return document.createElement("input");
    };

    Input.prototype.setType = function(type){
        this.element.type = type;
    };

    Input.prototype.setValue = function(value){
        this.element.value = value;
    };

    Input.prototype.setName = function(name){
        this.element.name = name;
    };

    var newInput = new Input("text", "", "qwe", "qwe");

    document.getElementById("container").appendChild(newInput);

</script>

变量输入=函数(类型、id、名称、值){
this.element=this.create();
这个.setType(type);
这个.setValue(值);
这个.setName(name);
返回此.element;
};
Input.prototype.create=函数(){
返回文档.createElement(“输入”);
};
Input.prototype.setType=函数(类型){
this.element.type=类型;
};
Input.prototype.setValue=函数(值){
this.element.value=值;
};
Input.prototype.setName=函数(名称){
this.element.name=名称;
};
var newInput=新输入(“文本”、“qwe”、“qwe”);
document.getElementById(“容器”).appendChild(newInput);

那个
这个
是只读的。非常感谢你,你的代码就是我要找的!!!我在您的代码中看到,我忘记了
返回此.element
行,现在使用
返回此.element
行,警报(newInput);完全返回[object HTMLInputElement],这就是我想要的。所以问题解决了,再次感谢你