如何使用 Prototype 向“body"添加元素? [英] How to add an element to 'body' using Prototype?
本文介绍了如何使用 Prototype 向“body"添加元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个这样的元素:
I have created an element like this:
var myDiv = new Element('div');
myDiv.update('Hello!');
我想将 myDiv
添加到正文.
I want to add myDiv
to body.
我试过了
$('body').insert(myDiv);
但是它不起作用.我也试过
But it is not working. I also tried
$('body')[0].insert(myDiv);
认为 $('body')
正在返回一个数组.即使那样也没有用.
thinking that $('body')
was returning an array. Even that didn't work.
如何将 myDiv
添加到正文?
How can I add myDiv
to body?
谢谢.
推荐答案
$ 是 document.getElementById() 的简写,$$ 是更通用的原型函数.要访问文档中的(第一个)body 元素,请使用:
$ is a shorthand for document.getElementById(), $$ is the more versatile prototype function. To access the (first) body element in your document, use:
$$('body')[0].insert(myDiv);
这篇关于如何使用 Prototype 向“body"添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文