0

我正在尝试在 div 上动态设置宽度。这适用于 Safari、Chrome 和 Firefox,但不适用于 IE8。这是使用 jQuery UI 的滑块功能。到目前为止,这是我的代码:

var ourStoryPosts = $(".ourStory .grid_12mod").children();
var ourStoryLength = ourStoryPosts.length;
var ourStoryWidth = $(ourStoryPosts[2]).outerWidth(true);
var ourStoryWrapperWidth = ourStoryLength * ourStoryWidth;
var maxSlider = ourStoryWrapperWidth - 1020;

$(".ourStory .grid_12mod").width(ourStoryWrapperWidth);

$(".ourStorySlider").slider({ 
    step: 1, 
    max: maxSlider,
    slide: function( event, ui) {
        $(".ourStory").css({
            "left" : -ui.value
        });
    }
});

如您所见,我使用变量来设置这些值。该width()方法未按预期工作。

我尝试使用String()构造函数显式转换为字符串。我也尝试+ "px"在表达式的末尾使用。每次, div 都被设置为0px. 为什么这不能正常工作?

4

2 回答 2

0

Not sure what the actual issue was, but using native JS fixed it for me:

var elem = document.querySelector(".grid_12mod");
var ourStoryChildren = elem.children;

Using the jQuery 1.9 method of children() logged a result of 5 elements in Safari, Chrome and Firefox, while IE8 logged 1. Using the native children attribute of the elem object fixed the problem and logged 5 for all browsers.

The reason this was logging 0 is because IE8 was returning only 1 child element, when 5 existed, therefore the math was off and IE8 was trying to find the second array element when it only found 1 element.

于 2013-03-06T16:19:33.570 回答
0

您可以尝试设置css属性宽度吗?

$(".ourStory .grid_12mod").css('width', ourStoryWrapperWidth.toString() + 'px');
于 2013-03-06T15:50:20.033 回答