div中添加一个文本框用于输入高 一个输入宽再向div添加一个按钮 当用户点击按钮 div高宽变为输入的值用js
展开全部
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
#test {
border:1px solid #444;
background:#dfdfdf;
width:200px;
height:200px;
}
</style>
</head>
<body>
<div id="test">
<form name="form1">
宽:<input type="text" name="div_width" >
高:<input type="text" name="div_height" >
<input type="button" onclick="SetSize();" value="设置">
</form>
</div>
</body>
<script>
function SetSize() {
div = document.getElementById('test');
div.style.width = document.form1.div_width.value + 'px';
div.style.height = document.form1.div_height.value + 'px';
}
</script>
</html>
试试看是否符合需求。
追问
不要form可以吗?
追答
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
#test {
border:1px solid #444;
background:#dfdfdf;
width:200px;
height:200px;
}
</style>
</head>
<body>
<div id="test">
<form name="form1">
宽:<input type="text" id="div_width">
高:<input type="text" id="div_height" >
<input type="button" onclick="SetSize();" value="设置">
</form>
</div>
</body>
<script>
function SetSize() {
div = document.getElementById('test');
div_width = document.getElementById('div_width').value;
div_height = document.getElementById('div_height').value;
div.style.width = div_width + 'px';
div.style.height = div_height + 'px';
}
</script>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询