简体   繁体   English

JQuery .css()返回100%为100px

[英]JQuery .css() returning 100% as 100px

<style>
.info{
        width:94%;
      }
</style>

Now doing like this using JQuery 现在使用JQuery做这样的事情

$('.info').css('width');

returns 94px rather than the equivalent value in pixles ( 500px in my case.) 返回94px而不是pixles中的等效值(在我的情况下是500px 。)

if i set the width in css as 105% , JQuery would return it as 105px . 如果我将css中的宽度设置为105% ,JQuery会将其返回为105px

The height and width of the parent container is 600px and 500px respectively. 父容器的高度和宽度分别为600px和500px。

I think a possible cause is the container of the element being hidden at the moment you're trying to obtain the dimensions. 我认为可能的原因是在您尝试获取尺寸时隐藏元素的容器。

I had a similar situation with nested divs and tables. 我有嵌套div和表的类似情况。

See the following fiddle where I managed to reproduce the behavior: 请参阅以下小提琴,我设法重现行为:

http://jsfiddle.net/36yvb/ http://jsfiddle.net/36yvb/

I still think it's a bug in jQuery returning a percentage as an integer number (without the % sign) when not being able to actually compute the dimensions of the element. 我仍然认为这是jQuery中的一个错误,当不能实际计算元素的维度时,将百分比作为整数返回(没有%符号)。 Change the width of #another-cell (expressed in %) and see it return the same value without the % sign when the table is hidden. 更改#another-cell的宽度(以%表示),并在隐藏表时看到它返回相同的值而不带%符号。

Actually, it doesn't seem so: see this fiddle . 实际上,它似乎并非如此: 看到这个小提琴

html: HTML:

<div id="info"></div>

css: CSS:

html, body {
    position: relative;
    width: 500px;
}

#info {        
    width: 94%;
}

js: JS:

$( function() {
    document.write( $('#info').css('width') );
});

what is info width relative to? 什么是信息宽度相对于? is it an element with 100px width maybe? 是宽度为100px的元素吗?

The width that it returns depends upon what part of the HTML is it written in. Meaning to say is that if it is written inside tag then then 94% percent would reflect 94% of that particular div. 它返回的宽度取决于它所写入的HTML的哪个部分。意思是说,如果它写在标签内,则94%的百分比将反映该特定div的94%。

But again if another tag in the same tag is already using some amount of defined width then the info might even get lesser space if the tag in defined before the info tag. 但是,如果同一标签中的另一个标签已经使用了一定数量的定义宽度,那么如果在信息标签之前定义的标签,信息甚至可能会获得更小的空间。

Please give up more amount of actual code to get a better answer 请放弃更多实际代码以获得更好的答案

The only thing that explains this behavior is that you have more than one element with the class info 解释这种行为的唯一方法是你有多个带有类info元素

When this happens and you do a $('.info').css('width'); 当发生这种情况并且你做$('.info').css('width'); jQuery will return to you the width of the first element in the set. jQuery将返回集合中第一个元素的width

If this is the case, you may need to be more specific with your selector. 如果是这种情况,您可能需要更具体地使用选择器。

DEMO DEMO

Each broswer will return textually different, but logically equal values eg, #FFF, #ffffff, and rgb(255,255,255) when using .css() 当使用.css()时,每个broswer将返回文本上不同但逻辑上相等的值,例如#FFF,#fffff和rgb(255,255,255)

Instead of using .css('width') use .width() 而不是使用.css('width')使用.width()

According to jQuery: http://api.jquery.com/width/ 根据jQuery: http//api.jquery.com/width/

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). .css(width)和.width()之间的区别在于后者返回无单位像素值(例如,400),而前者返回单位完整的值(例如,400px)。 The .width() method is recommended when an element's width needs to be used in a mathematical calculation. 当需要在数学计算中使用元素的宽度时,建议使用.width()方法。

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

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