简体   繁体   English

在Firefox中无法设置element.style.float?

[英]Can't set element.style.float in Firefox?

I've posted the following javascript code as a fiddle . 我发布了以下javascript代码作为小提琴 It's pretty basic, and works fine in IE11 and in Chrome, but FireFox quietly throws away my float settings. 这是非常基本的,在IE11和Chrome中运行良好,但FireFox悄然抛弃了我的float设置。 When I inspect the elements with FireBug, neither of them have float set in their style. 当我使用FireBug检查元素时,它们都没有float样式。

Why? 为什么?

Surfing the web, I've gotten the vague impression that it has something to do with width needing to be set somewhere, but where? 在网上冲浪,我得到的模糊印象是它与width需要设置在某处有关,但在哪里? And if that's the issue, is there a way for me to do this in firefox without knowing the width of the container? 如果这是问题,我有没有办法在Firefox中执行此操作而不知道容器的宽度?

var titleElem = document.createElement("div");
    titleElem.innerHTML = "Testing";
    titleElem.setAttribute("unselectable", "on");
    titleElem.style.border = "0px transparent";
    titleElem.style.width = "auto";
    titleElem.style.float = "left";
    titleElem.style.whiteSpace = "nowrap";
    titleElem.style.overflow = "hidden";
    titleElem.style.position = "relative";

document.body.appendChild(titleElem);

    rangeElem = document.createElement("div");
    rangeElem.setAttribute("unselectable", "on");
    rangeElem.style.width = "auto";
    rangeElem.style.border = "0px transparent";
    rangeElem.style.float = "right";
    rangeElem.style.whiteSpace = "nowrap";
    rangeElem.style.overflow = "hidden";
    rangeElem.style.position = "relative";
    rangeElem.style.fontSize = "x-small";
    rangeElem.style.paddingRight = "5px";
    rangeElem.className = "rangeDiv";
    rangeElem.innerHTML = "<i>[1234 - 5678]</i>";

document.body.appendChild(rangeElem);

Try changing that line to 尝试将该行更改为

titleElem.style.setProperty('float', 'left')

Or, 要么,

rangeElem.style.cssFloat = 'left'

also works. 也有效。

You use .cssFloat to access that style property. 您使用.cssFloat访问该样式属性。

titleElem.style.cssFloat = "left";

Apparently, this has to do with float being a reserved word in javascript. 显然,这与float是javascript中的保留字有关。

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

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