jquery / javascript 设置宽度失败

发布于 2024-08-12 14:16:56 字数 1040 浏览 7 评论 0原文

我遇到过这个问题: 根据我想创建一个条形图的数字。有趣的是,它在设置为怪异模式的 IE8 中运行得很好,但在其他地方却失败了。我真的不能说错误在哪里,我希望这里有人可以帮助我。我正在使用 jQuery,但使用 getElementById() 和 element.style.width = Somevalue (即没有 jQuery)也不起作用:(

[编辑:完整示例链接已删除;粘贴已过期。]

基本上:

<input onChange="calculateField(1)" ...>

function calculateField(fieldname){
    value = $("#input_" + fieldname).val();
    fancyMagic();
    value = 6 * value; // for testing

    $("#subtotal_" + fieldname).html(value);
    updateDiagram();
}

function updateDiagram(){
    // gather required into, maxwidth, maxval, etc...

    // fetch 'normal' bar
    var target = $("#animatebar_1");
    var width = maxwidth * (value / maxval);

    // set width to proper width
    target.width(width);

    // like for avg bar
    var avgtarget = $("#avgbar_1");
    var avgwidth = maxwidth * (averagevalue / maxval);
    avgtarget.width(avgwidth);
}

我尝试了所有 FF, IE8 在各种设置和 Opera 中都不起作用,IE8 怪异模式之后它就不起作用了,有趣的是,

我很确定这只是我的愚蠢,但我将不胜感激。也尝试使用 .css() 来更改大小,但也没有成功,

非常感谢您。

I have encountered this problem:
Depending on numbers from I want to create a bar chart. Interestingly it works very well in IE8 set on quirks mode, but fails everywhere else. I cannot really say where the error is and I hope that someone here can help me. I am using jQuery, but using getElementById() and element.style.width = Somevalue (i.e. without jQuery) did not work either :(

[Edit: Full example link removed; the pastie expired.]

Basically:

<input onChange="calculateField(1)" ...>

function calculateField(fieldname){
    value = $("#input_" + fieldname).val();
    fancyMagic();
    value = 6 * value; // for testing

    $("#subtotal_" + fieldname).html(value);
    updateDiagram();
}

function updateDiagram(){
    // gather required into, maxwidth, maxval, etc...

    // fetch 'normal' bar
    var target = $("#animatebar_1");
    var width = maxwidth * (value / maxval);

    // set width to proper width
    target.width(width);

    // like for avg bar
    var avgtarget = $("#avgbar_1");
    var avgwidth = maxwidth * (averagevalue / maxval);
    avgtarget.width(avgwidth);
}

I tried all of FF, IE8 in various settings and Opera and it simply doesn't work. The bar's width is set to zero immediately after. IE8 quirks mode works, interestingly.

I am pretty sure it's just me being dumb, but I will appreciate help with this. I also tried to use .css() to change the size, but it did not work either.

Thank you muchly in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

潜移默化 2024-08-19 14:16:56

我认为问题可能是跨度标签的使用。你应该使用 div 来代替。 Span 标签忽略宽度。

I think the problem might be the use of the span tags. You should use a div instead. Span tags ignore widths.

凉城凉梦凉人心 2024-08-19 14:16:56

解决办法其实很简单。除 IE 之外的其他浏览器都正确遵循盒模型。这意味着您无法设置内联元素的宽度。您使用的栏是 SPAN 元素,并且它们设置为 display:inline

因此解决方案是使用 DIV 而不是 SPAN,或者为 .animbars 和 .animbars 添加 display:block .avgbars CSS 声明。它可能会破坏您预期的视觉设计,但让它回到正轨应该不难。

the solution is actually very easy. Other browsers than IE follow the box-model correctly. This means that you can't set a width of an inlined element. The bars you are using are SPAN elements and these are set to display:inline

So the solution would be using DIV's instead of SPAN's or adding display:block for both .animbars and .avgbars CSS declarations. It may break your intended visiual design but it shouldn't be so hard to get it back on tracks.

笨笨の傻瓜 2024-08-19 14:16:56

你从哪里得到 maxval?它可以是 null、nan 或零。不管怎样,你都会得到一个错误。

where do u get maxval? it either null,nan or zero. either way u get an error there.

静若繁花 2024-08-19 14:16:56

尝试添加 target.css('width', width + "px")

除此之外,我看不到任何明显的错误。

try adding target.css('width', width + "px")

Other than that, i can't see any obvious errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文