JavaScript的获得方式和增量 [英] javascript get style and increment

查看:242
本文介绍了JavaScript的获得方式和增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出改变的onclick在外部CSS的利润率左属性的滑块。所以,我需要得到的属性,然后设置新的属性。

I'm trying to make a slider that changes the margin-left attribute in the external css onclick. So I need to get the attribute and then set the new attribute.

这是我的code:

<style>
#divName
 {
  margin-left:20px;
 }
</style>

    <script language="javascript" type="text/javascript">  

                var x = document.getElementById(divName);

            if (x.currentStyle)
            {
                var y = x.currentStyle[marginLeft];
            }
            else (window.getComputedStyle)
            {
                var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(marginLeft);
            }

                y = y + 20;

                document.getElementById(divName).style.marginLeft = y .'px';

    </script>

感谢您的时间

推荐答案

在JavaScript中,运营商不连接字符串。

In Javascript, the . operator does not concatenate strings.

使用 + 操盘手:

document.getElementById(divName).style.marginLeft = y + 'px';

您还可能需要通过 marginLeft 作为一个字符串,而不是一个名字(除非有作用域的变量名为 marginLeft ,并设置为marginLeft

You also probably need to pass marginLeft as a string, not a name (unless there is a variable in scope that is named marginLeft and set to "marginLeft"):

var y = x.currentStyle["marginLeft"];

var y = document.defaultView.getComputedStyle(x, null).getPropertyValue("marginLeft");

这篇关于JavaScript的获得方式和增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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