Bill0123
2015-08-11 09:35
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
#txt{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
<h2 id="con">JavaScript课程</H2>
<div id="txt">
<h5>JavaScript为网页添加动态效果并实现与用户交互的功能。</h5>
<p>1. JavaScript入门篇,让不懂JS的你,快速了解JS。</p>
<p>2. JavaScript进阶篇,让你掌握JS的基础语法、函数、数组、事件、内置对象、BOM浏览器、DOM操作。</p>
<p>3. 学完以上两门基础课后,在深入学习JavaScript的变量作用域、事件、对象、运动、cookie、正则表达式、ajax等课程。</p>
</div>
<form>
<!--当点击相应按钮,执行相应操作,为按钮添加相应事件-->
<input type="button" value="改变颜色" onclick="changecolor()">
<input type="button" value="改变宽高" onclick="changewidth()">
<input type="button" value="隐藏内容" onclick="hidden()">
<input type="button" value="显示内容" onclick="show()">
<input type="button" value="取消设置" onclick="cancel()">
</form>
<script type="text/javascript">
function changecolor(){
var obj=getElementById("txt")
obj.style.color="red";
obj.style.backgroundColor="yellow";
}//定义"改变颜色"的函数
function changewidth(){
var obj=getElementById("txt")
obj.style.width="500"
obj.style.height="400"
}//定义"改变宽高"的函数
function hidden(){
var obj=getElementById("txt")
obj.style.display="none"
}//定义"隐藏内容"的函数
function show(){
obj.style.display="block"
}//定义"显示内容"的函数
function cancel(){
var obj=confirm("是否取消设置?")
if(obj==true){
obj.removeAttribute("style");
}
}//定义"取消设置"的函数
</script>
</body>
</html>为什么除了最后一个其他效果都出不来呢??
var obj=getElementById("txt")这一句缺个document.
var obj=document.getElementById("txt")obj.style.width="500" obj.style.height="400"
这2行缺了px
obj.style.width="500px" obj.style.height="400px"
<input type="button" value="隐藏内容" onclick="hidden()">
function hidden(){函数名不要取js的关键字,换个名吧
<input type="button" value="隐藏内容" onclick="hide()">
function hide(){function cancel(){
var obj=confirm("是否取消设置?")
if(obj==true){
obj.removeAttribute("style");
}
}//定义"取消设置"的函数这里重置的应该是id为txt的样式,你这样成重置对话框的样式。
function cancel(){
var obj=document.getElementById("txt")
var message=confirm("是否取消设置?")
if(message==true){
obj.removeAttribute("style");
}
}//定义"取消设置"的函数修改后的全代码(在你原先的代码基础上改的,我就没把变量提出来了哈)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
#txt{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
<h2 id="con">JavaScript课程</H2>
<div id="txt">
<h5>JavaScript为网页添加动态效果并实现与用户交互的功能。</h5>
<p>1. JavaScript入门篇,让不懂JS的你,快速了解JS。</p>
<p>2. JavaScript进阶篇,让你掌握JS的基础语法、函数、数组、事件、内置对象、BOM浏览器、DOM操作。</p>
<p>3. 学完以上两门基础课后,在深入学习JavaScript的变量作用域、事件、对象、运动、cookie、正则表达式、ajax等课程。</p>
</div>
<form>
<!--当点击相应按钮,执行相应操作,为按钮添加相应事件-->
<input type="button" value="改变颜色" onclick="changecolor()">
<input type="button" value="改变宽高" onclick="changewidth()">
<input type="button" value="隐藏内容" onclick="hide()">
<input type="button" value="显示内容" onclick="show()">
<input type="button" value="取消设置" onclick="cancel()">
</form>
<script type="text/javascript">
function changecolor(){
var obj=document.getElementById("txt")
obj.style.color="red";
obj.style.backgroundColor="yellow";
}//定义"改变颜色"的函数
function changewidth(){
var obj=document.getElementById("txt")
obj.style.width="500px"
obj.style.height="400px"
}//定义"改变宽高"的函数
function hide(){
var obj=document.getElementById("txt")
obj.style.display="none"
}//定义"隐藏内容"的函数
function show(){
var obj=document.getElementById("txt")
obj.style.display="block"
}//定义"显示内容"的函数
function cancel(){
var obj=document.getElementById("txt")
var message=confirm("是否取消设置?")
if(message==true){
obj.removeAttribute("style");
}
}//定义"取消设置"的函数
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
<title>javascript</title>
<style type="text/css">
body{font-size:12px;}
#txt{
height:400px;
width:600px;
border:#333 solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
<h2 id="con">JavaScript课程</H2>
<div id="txt">
<h5>JavaScript为网页添加动态效果并实现与用户交互的功能。</h5>
<p>1. JavaScript入门篇,让不懂JS的你,快速了解JS。</p>
<p>2. JavaScript进阶篇,让你掌握JS的基础语法、函数、数组、事件、内置对象、BOM浏览器、DOM操作。</p>
<p>3. 学完以上两门基础课后,在深入学习JavaScript的变量作用域、事件、对象、运动、cookie、正则表达式、ajax等课程。</p>
</div>
<form>
<!--当点击相应按钮,执行相应操作,为按钮添加相应事件-->
<input type="button" value="改变颜色" onclick="changecolor()">
<input type="button" value="改变宽高" onclick="changewidth()" >
<input type="button" value="隐藏内容" onclick="hidden()">
<input type="button" value="显示内容" onclick="show()">
<input type="button" value="取消设置" onclick="cancel()">
</form>
<script type="text/javascript">
var obj=document.getElementById("txt");
function changecolor(){
obj.style.color="red";
obj.style.backgroundColor="yellow";
}//定义"改变颜色"的函数
function changewidth(){
obj.style.width="500"
obj.style.height="400"
}//定义"改变宽高"的函数
function hidden(){
obj.style.display="none"
}//定义"隐藏内容"的函数
function show(){
obj.style.display="block"
}//定义"显示内容"的函数
function cancel(){
var obj=confirm("是否取消设置?")
if(obj==true){
obj.removeAttribute("style");
}
}//定义"取消设置"的函数
</script>
</body>我把变量放到最外面了,为啥这样还是不行啊?/(ㄒoㄒ)/
function changecolor(){
var obj=getElementById("txt")
obj.style.color="red";
obj.style.backgroundColor="yellow";
}//定义"改变颜色"的函数
//粗体部分少东西
var obj=document.getElementById("txt");
JavaScript入门篇
741595 学习 · 9869 问题
相似问题