dom模式中的新建element元素一般是通过document.createElement方法来获得,但是我发现还有一个特殊的地方。
var img=new Image();
img.src="http://xxxxxx";
document.lastChild.lastChild.appendChild(img);
以上代码是通过new新建对象的方法来得到特殊element,于是我想能不能自己建立一个类似的类,然后通过new操作来得到相应的element元素,以下是代码
var Anchor=function(){};//新建锚链接类
Anchor.prototype=document.createElement("a");
var a=new Anchor();
a.innerText="123";
a.href="http://xxxxx";
document.lastChild.lastChild.appendChild(a);//Error!!!
新建出来的对象a是element的类型,可就是不能通过appendChild方法来添加到页面,提示参数类型错误。我希望通过类似new Anchor()之类的方法来构建一个锚链接对象,然后通过appendChild来把他加入document,最后显示在浏览器中,我的代码到底哪里出了问题,或各位大大们有什么高招,请指教!!
var img=new Image();
img.src="http://xxxxxx";
document.lastChild.lastChild.appendChild(img);
以上代码是通过new新建对象的方法来得到特殊element,于是我想能不能自己建立一个类似的类,然后通过new操作来得到相应的element元素,以下是代码
var Anchor=function(){};//新建锚链接类
Anchor.prototype=document.createElement("a");
var a=new Anchor();
a.innerText="123";
a.href="http://xxxxx";
document.lastChild.lastChild.appendChild(a);//Error!!!
新建出来的对象a是element的类型,可就是不能通过appendChild方法来添加到页面,提示参数类型错误。我希望通过类似new Anchor()之类的方法来构建一个锚链接对象,然后通过appendChild来把他加入document,最后显示在浏览器中,我的代码到底哪里出了问题,或各位大大们有什么高招,请指教!!
解决方案 »
- [求助] javascript定义类的函数加prototype是什么意思?
- onmouseDown 应用
- 怎么禁止checkbox选项框为只读,类似input type=text readonly的效果。readonly对checkbox不起作用。使用disabled会使该选项值也无效
- 伸缩效果
- 在线等待:_如何在网页上放一个表格
- 刚才忘了给分,请看看这个问题
- 请教关于捕获子窗口或页内框架的鼠标事件的问题:-)
- 如何清除IE地址栏里的某一条记录
- javascript里的变量问题
- 关于前端一些数据加密问题请教
- jquery 不需要按提交按钮能不能让#result获取选项值?
- 这样创建XMLHTTP错哪里了呢?
<script>
var Anchor = function(){
return document.createElement("a");
};//新建锚链接类window.onload=function(){
var a=new Anchor();
a.innerHTML="123";
a.href="http://xxxxx";
document.getElementById("xx").appendChild(a);
}
</script><div id="xx"></div>
var a=new Anchor("123","http://xxxxxxxxx");
document.lastChild.lastChild.appendChild(a);
就是自定义Anchor的构造函数。
{
this._hyperlink = document.createElement("a");
this._hyperlink.innerHTML = text;
this._hyperlink.href = uri;
this._hyperlink.className = className;
} anchor.prototype.append = function(containerNode)
{
containerNode.appendChild(this._hyperlink);
} window.onload = function()
{
var oelement = new anchor("sohu.com", "http://www.sohu.com", "");
oelement.append(document.body);
}