[英]How do I change the width of an element using Javascript?
How do I change width
value assigned in #center
? 如何更改
#center
指定的width
值? (I don't want to switch between CSS files just because 1 changed width
value.) How do I dynamically change the value using Javascript? (我不想仅仅因为1更改了
width
值而在CSS文件之间切换。)如何使用Javascript动态更改值?
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style type="text/css">
#main #center {
width: 600px; // change width to 700 on click
float: left;
}
#main #center .send {
background: #fff;
padding: 10px;
position: relative;
}
#main #center .send .music {
text-align: center;
margin-bottom: 10px;
}
#main #center .send .write {
font-family: georgia, serif;
font-size: 150px;
opacity: 0.2;
}
//....etc.
</style>
</head>
<body>
// change #center width to 700
<a href="#go2" onclick="how to change width of #center ???" ></a>
</body>
</html>
document.getElementById('center').style.width = "599px";
Two possible ways: 两种可能的方式:
document.getElementById( 'center' ).style.width = "700px";
Call a javascript function in the onclick which takes the dom element and sets the new width OR changes the class of the element 在onclick中调用一个javascript函数,它接受dom元素并设置新的宽度或更改元素的类
function changeWidth(){
document.getElementById('center');
div.style.width="1270px";
}
<div onclick="changeWidth()" id="main">
Stuff
</div>
<div onclick="changeWidth()" id="center">
More Stuff
</div>
function changeWidth(){
this.style.width = "700px";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.