扩展DOM元素的功能

时间:2016-03-15 05:55:22

标签: javascript dom

如何选择DOM元素并将一系列操作链接到元素,如下所示:

-(instancetype)initWithTweet:(PCRTweet *)tweet reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super init];

if (self) {
    _tweet = tweet;

    reuse = reuseIdentifier;

    CGSize cellSize = self.contentView.frame.size;

    CGRect backgroundView = CGRectMake(10.f, 5.f, (cellSize.width-20.f), (cellSize.height + 90.f));


    self.view = [[UIView alloc] initWithFrame:backgroundView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.layer.cornerRadius = 8;
    self.view.layer.masksToBounds = YES;
    self.view.layer.borderWidth = 1.0f;
    self.view.layer.borderColor = background_color_gray.CGColor;
    self.view.layer.masksToBounds = NO;

    [self.contentView addSubview:self.view];

    CGRect picView = CGRectMake(0.f, 0.f, 65.f, 65.f);

    self.contentView.backgroundColor = background_color_gray;

}

return self;
}

我意识到jQuery和其他许多库都在做上述事情,但我有点失落......

实际的实现不是必需的,只是选择和链接的基本框架。

1 个答案:

答案 0 :(得分:-1)

使用jQuery可以链接方法:

$('#maindiv')
    .css({left: 100, top: 250})
    .fadeOut(1000)
    .addClass('.foo')
    .toggleClass('.inactive', $(this).is(':checked'))
;

检查这些方法,看看如何进行链接。

通常你会使像function _(selector)这样的函数返回所选元素对象及其内部的所有新函数。

function _(selector) {
    var obj = {
        el: document.getElementById(selector),
        css: function (attributes, values) {
            /* ... */

            return obj;
        },
        fadeOut: function (speed) {
            /* ... */

            return obj;
        }
    };

    return obj;
}
相关问题