如何更改width
中分配的#center
值? (我不想仅仅因为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>
答案 0 :(得分:3)
document.getElementById('center').style.width = "599px";
答案 1 :(得分:2)
两种可能的方式:
答案 2 :(得分:1)
document.getElementById( 'center' ).style.width = "700px";
答案 3 :(得分:0)
在onclick中调用一个javascript函数,它接受dom元素并设置新的宽度或更改元素的类
function changeWidth(){
document.getElementById('center');
div.style.width="1270px";
}
答案 4 :(得分:0)
<div onclick="changeWidth()" id="main">
Stuff
</div>
<div onclick="changeWidth()" id="center">
More Stuff
</div>
function changeWidth(){
this.style.width = "700px";
}