简体   繁体   中英

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

I have a div defined with a style attribute:

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

How can I change the height of the div with JavaScript?

<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. Changing height in dojo would be done with something similiar to the following:

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

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:

<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;

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...

In dojo, you would do it like this:

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

Having the units on the height is important, as mentioned in the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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