Ross Wan's World!

Python, Ajax, PHP and Linux.

Mootools: toElement 方法

Posted by Ross Wan 于 2009/01/31

toElement 是 Mootools 1.2 里的一个新特性(但并没有在其官方文档里说明)。toElement 只是一个返回 element 对象的方法。当向 $ 传递一个对象时,它会尝试调用对象的 toElement 方法来取得当前 element。


var User = new Class({
    initialize: function(name){ = name;

    toElement: function(){
        if (!this.element) {
            this.element = new Element('div', {'class': 'user'});
            var avatar = new Element('img', {
                src: '/avatars/' + + '.jpg'

            var description = new Element('div', {
                'class': 'username',
            this.element.adopt(avatar, description);
        return this.element;

window.addEvent('domready', function(){
    var alice = new User('Alice');
    var bob = new User('Bob');
    $(alice).inject('header', 'after');
    // let's inject bob after alice
    $(bob).inject(alice, 'after');


Fill in your details below or click an icon to log in: 徽标

You are commenting using your account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s

%d 博主赞过: