简体   繁体   English

为什么jQuery会为元素的css函数返回undefined?

[英]Why would jQuery return undefined for the css function of an element?

jQuery 1.11.1, on Mac OS X Mavericks, latest version of Safari. jQuery 1.11.1,在 Mac OS X Mavericks 上,Safari 的最新版本。

I'm trying to set CSS properties with the css function.我正在尝试使用 css 函数设置 CSS 属性。 css() is missing from elements.元素中缺少 css()。

First, I validated that the element is selected correctly:首先,我验证是否正确选择了元素:

// There is only one element that has id="container"
var $container = $('#container');

// Selector returns a collection. I have to access first element:
console.log($container[0]); // prints HTMLDivElement

// css function is undefined
console.log($container[0].css); // prints undefined

// Also can't set any css value. Gives undefined error.
$container[0].css({backgroundColor:'blue'});

The error is:错误是:

TypeError: 'undefined' is not a function (evaluating '$container[0].css({backgroundColor:'blue'})') TypeError: 'undefined' 不是一个函数(评估 '$container[0].css({backgroundColor:'blue'})')

I removed everything from the test page.我从测试页中删除了所有内容。 It includes jQuery, and within the body it has the div with the ID.它包含 jQuery,并且在正文中包含带有 ID 的 div。 Very simple.很简单。 Beneath that, there is the JavaScript code shown above.在它下面,是上面显示的 JavaScript 代码。

Any idea why the div would be lacking the css function?知道为什么 div 会缺少 css 功能吗?

It is because you are dropping out of the jQuery object and are using the DOM element...这是因为您正在退出 jQuery 对象并使用 DOM 元素...

$container[0].css({backgroundColor:'blue'});

The [0] here gets you the DOM object from the jQuery object.这里的[0]从 jQuery 对象中获取 DOM 对象。

Use jQuery if you want to access the jQuery method...如果要访问 jQuery 方法,请使用 jQuery...

$container.css({backgroundColor:'blue'});

You're using jQuery incorrectly.您错误地使用了 jQuery。 When you say $container[0] you are getting the first javascript DOM element of the jQuery object (which doesn't have any jquery functions attached to it).当您说$container[0]您将获得 jQuery 对象的第一个 javascript DOM 元素(它没有附加任何 jquery 函数)。 If you want to get the css background color of the element using jQuery you need to do $container.css("background-color") , and to set it $container.css("background-color", "blue");如果你想使用 jQuery 获取元素的 css 背景颜色,你需要做$container.css("background-color") ,并设置它$container.css("background-color", "blue"); or $container.css({ "background-color": "blue" });$container.css({ "background-color": "blue" });

Because the css function is a method of a jquery object.因为css函数是一个jquery对象的方法。 When you do $container[0] you get the first DOM node that matched the selector, which is not a jquery object.当您执行$container[0]您将获得与选择器匹配的第一个 DOM 节点,它不是 jquery 对象。

Try $container.css(...) .试试$container.css(...)

When you access the collection items, no longer has the jQuery methods, only the DOM elements.当您访问集合项时,不再有 jQuery 方法,只有 DOM 元素。

You could replace:你可以替换:

$container[0].css({backgroundColor:'blue'});

by经过

$container.css({backgroundColor:'blue'});

If you have one more div element with the same css attribute,如果您还有一个具有相同 css 属性的 div 元素,

for instance lets say below statement returns more than one result:例如让我们说下面的语句返回多个结果:

var $container = $('.container');  // Has 3 results

And you want to reach specific elements css attribute then you can revert the dom element into jquery element and do what you want as below:并且您想访问特定元素的 css 属性,然后您可以将 dom 元素恢复为 jquery 元素并执行您想要的操作,如下所示:

$($('.container').get(2)).css({ "background-color": "blue" });

http://jsfiddle.net/JNR63/ http://jsfiddle.net/JNR63/

check this example检查这个例子

alert($container.css("width")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM