简体   繁体   English

在获取元素的边框宽度之前,如何确保添加了一个类?

[英]How do I make sure a class is added before getting the border width of the element?

It seems that when I use jQuery's addClass function adding a class with a 2px border, that it returns the old value at times and not the new when outputting the css in an iframe. 看来,当我使用jQuery的addClass函数添加具有2px边框的类时,在iframe中输出css时,它有时会返回旧值,而不是新值。 I assume that this happens only sometimes due to how fast the class gets added. 我认为这仅在某些情况下会发生,这是由于添加类的速度。 For example: 例如:

Stylesheet: 样式表:

.testBorder {
   border: 2px solid #000;
}

JavaScript: JavaScript:

var iframe = $('#contentFrame').contents();
var obj = $('#someObj',iframe);
obj.addClass('testBorder');
console.log(obj.css('border-top-width')); 
console.log(obj.css('border-left-width'));

The border values outputted are often 0 instead of 2px. 输出的边框值通常为0,而不是2px。 I can do something like: 我可以做类似的事情:

obj.delay(500).queue(function(){
   console.log($(this).css('border-top-width')); 
   console.log($(this).css('border-left-width'));
   $(this).dequeue();
});

But looking to apply it immediately after the class is added as I use the values to positon correctly to not look like its jumping. 但是我希望在添加类后立即应用它,因为我使用了正确的位置以使其看起来不像跳跃。

The CSS is being applied and is reflected, so I know it is updating and not being overridden by another style. CSS正在被应用并被反映出来,所以我知道它正在更新并且不会被另一种样式覆盖。

The jQuery version I am using is 1.10.2 with the jQuery Migrate v1.2.1 and I am using the jQuery UI. 我正在使用的jQuery版本是jQuery Migrate v1.2.1的1.10.2,我正在使用jQuery UI。

Try this: 尝试这个:

var iframe_content = ($.browser.msie ? $('#contentFrame').get(0).contentWindow.document) : $('#contentFrame').get(0).contentDocument));
var obj = iframe_content.find('#someObj');
obj.queue(function() { /** check for border width here **/ });
obj.addClass('testBorder').dequeue(); /* this last .dequeue() may not be necessary */

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

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