简体   繁体   English

jQuery .css()返回对象对象?

[英]jquery .css() returns object object?

I have something like this: 我有这样的事情:

    var windowH = $(window).height();
    var listH = $(".list").height();
    if(windowH > listH){
        $(".list").css({'height': windowH+'px'});   
        alert(windowH);
        alert($(".list").css({'height': windowH+'px'}));
    }

the second alert gives me [object Object] and therefore doesn't insert the correct css. 第二个警报给了我[object Object] ,因此没有插入正确的CSS。 can someone help? 有人可以帮忙吗?

Thanks 谢谢

A call to $(el).css(propertyName, value) returns the element you're working with,. 调用$(el).css(propertyName, value)返回您正在使用的元素。

This is so you can chain functions. 这样就可以链接功能。

$(".list").css(propertyName, value).css(propertyName, value).remove(); // etc etc.

windowH is most likely a number, try windowH.toString() + 'px' if you want something like "600px" . windowH最有可能是数字,如果您想要类似"600px" ,请尝试windowH.toString() + 'px'

.css method returns the jQuery object (For method chaining), so the alert result is nothing wrong. .css方法返回jQuery对象(用于方法链接),因此警报结果没有错。

Instead of using .css , try use .height method. 代替使用.css ,请尝试使用.height方法。

$(".list").height(windowH);

Use this: 用这个:

alert( $(".list").css('height') );

When you call the .css() method with a map of key/value pairs like in your code it assumes you are trying to set those CSS properties, and it returns the jQuery object you called it on (which alert() quite correctly displays as "[object Object]" ). 当您使用代码中的键/值对映射调用.css()方法时.css()假设您正在尝试设置这些CSS属性),并且它返回您调用它的jQuery对象( alert()正确显示)为"[object Object]" )。 That's why you can chain other jQuery methods after .css({...}) . 这就是为什么您可以在.css({...})之后链接其他jQuery方法。

If you call .css() with just the name of a particular property like I've shown above then it returns the value of that property so that will work within an alert() . 如果仅使用特定属性的名称(如上所示.css()调用.css() ,则它将返回该属性的值,以便可以在alert() (Note though that if your jQuery object has more than one element, ie, if ".list" matches multiple elements, .css('height') returns the height value for the first element only.) (但是请注意,如果您的jQuery对象具有多个元素,即,如果".list"匹配多个元素,则.css('height')返回第一个元素的高度值。)

EDIT: Note that this behaviour in the .css() method is the same as for other jQuery methods that can be used both to set and get "something", like .val() , .html() , .prop() , etc. When called with parameters that set a value these methods all return a jQuery object, but when called with parameters that get a value they all return the appropriate value. 编辑:请注意, .css()方法中的此行为与可用于设置和获取“内容”的其他jQuery方法相同,例如.val() .html() .prop() ,等等。当使用设置值的参数调用这些方法时,所有方法均返回jQuery对象,但是当使用获取值的参数调用时,它们均返回适当的值。

First, you have to set the property and then alert the appropriate values . 首先,您必须设置属性,然后警告相应的值。 .. ..

try 尝试

    var windowH = $(window).height();
var listH = $(".list").height();
if(windowH > listH){
    $(".list").css({'height': windowH+'px'});   
    alert(windowH);
    $(".list").css('height', windowH+'px');

    alert(listH);
} 

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

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