【问题标题】:jQuery find() throws `css method not found` errorjQuery find() 抛出`css method not found`错误
【发布时间】:2015-11-03 10:24:37
【问题描述】:

我有以下代码:

var done = function(el) {
        var tds = el.parent().parent().find('td');
        for (var i in tds) {
            tds[i].css('backgroundColor', 'green');
        }
    };
done($(this));

$(this) 指向 td 标签内的元素 - 所以我得到所有附近的 td 标签并更改它们的背景颜色。

问题是它会抛出tds[i].css函数未定义的错误。

在明确的 javascript 中执行此操作,并通过 this,可以完美地工作,如下所示:

var done = function(el) {
        var tds = el.parentNode.parentNode.getElementsByTagName('td');
        for (var i in tds) {
            tds[i].style.backgroundColor = 'green';
        }
    };
done(this);

怎么了?

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

也许试试这个:

var done = function(el) {
        var tds = el.parent().parent().find('td');

        $(tds).each(function(){
            this.css('backgroundColor', 'green');
        });
    };
done($(this));

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 2019-05-12
    • 2022-06-16
    • 1970-01-01
    • 2018-12-29
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多