简体   繁体   English

如何使用JavaScript更改style属性中的一个值?

[英]How can I change one value in style attribute with JavaScript?

I have a div defined with a style attribute: 我有一个使用style属性定义的div

<div id="div1" style="width:600;height:600;border:solid 1px"></div>

How can I change the height of the div with JavaScript? 如何用JavaScript更改div的高度?

<script type="text/javascript">
function changeHeight(height)
{
   document.getElementById("div1").style.height = height + "px";
}
</script>

Judging by his example code he is using the dojo framework. 从他的示例代码来看,他正在使用dojo框架。 Changing height in dojo would be done with something similiar to the following: 在dojo中改变高度将使用类似于以下内容的方式:

dojo.style("div1", "height", 300); 

http://api.dojotoolkit.org/jsdoc/dojo/1.2/dojo.style http://api.dojotoolkit.org/jsdoc/dojo/1.2/dojo.style

document.getElementById("div1").style.height = height + "px";

Here is how it might look with jQuery: 以下是jQuery的外观:

<div id="div1" style="width:600;height:600;border:solid 1px"></div>
<a href="#">Change height to 300</a>

<script type="text/javascript">
    $(function() {
        $('a').click(function() {
            $('#div1').css('height', '400px');
            return false;
        });
    });
</script>
var d = document.getElementById("div1");
d.style.height = "300px";

Just replace your comment with: 只需将评论替换为:

node.style.height = height; node.style.height = height;

Oh, not sure if just passing 300 to your function will make it work, perhaps you'll have to pass "300px" like suggested in the other posts... 哦,不确定是否只是将300传递给你的函数会使它工作,也许你必须像其他帖子中建议的那样传递“300px”...

In dojo, you would do it like this: 在dojo中,你会这样做:

dojo.style("div1", "height", "300px");

Having the units on the height is important, as mentioned in the docs . 文档中所述 ,将单位放在高度上很重要。

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

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