从JavaScript设置浮点值 [英] setting the float value from Javascript

查看:171
本文介绍了从JavaScript设置浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置某些东西在JS中浮动时遇到问题。我的代码如下:

I'm having trouble setting something to float right in JS. My code is as follows:

var closebutton = document.createElement('div');
closebutton.style.styleFloat = "right";
alert(closebutton.style.styleFloat);
closebutton.style.background = "#f00";
closebutton.innerHTML = '<a href="">&#10006;</a>';
titlebar.appendChild(closebutton);

元素的背景确实是红色,当加载页面警报右。然而,这个div不是漂浮的。 Firebug没有显示浮动的痕迹。错误控制台中没有错误或警告。

The background of the element is indeed red, and when loaded the page alerts "right". Yet the div is not floated right. Firebug shows no trace of the float. There are no errors or warnings in the Error Console.

我很抱歉!

更新:

根据建议,我也尝试过:

Update:
As suggested, I have also tried:

closebutton.style.float = 'right';

这也不起作用,在我的文本编辑器(gedit) p>

This also does not work, and is highlighted bright red in my text-editor (gedit)

推荐答案

在JavaScript中设置float样式的标准方法是使用 cssFloat property:

The standard way to set the float style in JavaScript is to use the cssFloat property:

closebutton.style.cssFloat = 'right';

这适用于所有浏览器,但不适用于IE,它预期 styleFloat 属性。因此,您可以检测浏览器并应用适当的属性,或者设置它们:

That works in all browsers, but not in IE, which expects the styleFloat property instead. Therefore you can either detect the browser, and apply the appropriate property, or else set them both:

closebutton.style.styleFloat = 'right';
closebutton.style.cssFloat = 'right';

请注意, float JavaScript,并且不能在点表示法中使用。这同样适用于 class ,例如,它是另一个保留字。这就是为什么与JavaScript保留字冲突的CSS属性有不同的名称()。

Note that float is a reserved word in JavaScript, and cannot be used in the dot notation. The same applies for class, for example, which is another reserved word. That's why there is a different name for CSS properties that conflict with JavaScript reserved words (Source).

进一步阅读:

  • Mozilla Dev Center: float CSS Proeprty

这篇关于从JavaScript设置浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆