<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
<!--
function dealclick(id1,id2){
document.write("1");
if (document.getElementById(id1).innerText =='-'){
document.write("2");
document.getElementById('id1').innerText='+';
document.getElementById('id2').style.display='none';}
else{
document.write("3");
document.getElementById('id1').innerText='-';
document.getElementById('id2').style.display='block';}
}
function test(){
document.write("调用了javascript");
}
//-->
</script>
</head><body>
<span id="b1" style="border:thin ridge;cursor:hand" onclick="dealclick('b1','list');">-</span>
list<ul id="list">
<li>item1</li>
</ul>
<script language="javascript">
document.write(document.getElementById('b1').innerText=='-');
document.write(b1.innerHTML=='-');
</script>
</body>
</html>
如上程序中,点击span,能进入函数,但进入不了if内,也不进else,为什么?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
<!--
function dealclick(id1,id2){
document.write("1");
if (document.getElementById(id1).innerText =='-'){
document.write("2");
document.getElementById('id1').innerText='+';
document.getElementById('id2').style.display='none';}
else{
document.write("3");
document.getElementById('id1').innerText='-';
document.getElementById('id2').style.display='block';}
}
function test(){
document.write("调用了javascript");
}
//-->
</script>
</head><body>
<span id="b1" style="border:thin ridge;cursor:hand" onclick="dealclick('b1','list');">-</span>
list<ul id="list">
<li>item1</li>
</ul>
<script language="javascript">
document.write(document.getElementById('b1').innerText=='-');
document.write(b1.innerHTML=='-');
</script>
</body>
</html>
如上程序中,点击span,能进入函数,但进入不了if内,也不进else,为什么?
而是你没有搞明白document.write()方法的使用。而出现了时间运行错误。
建议用alert();弹出框来就成!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
<!--
function dealclick(id1,id2){
alert("1");
if (document.getElementById(id1).innerText =='-'){
alert("2");
document.getElementById('id1').innerText='+';
document.getElementById('id2').style.display='none';}
else{
alert("3");
document.getElementById('id1').innerText='-';
document.getElementById('id2').style.display='block';}
}
function test(){
alert("调用了javascript");
}
//-->
</script>
</head><body>
<span id="b1" style="border:thin ridge;cursor:hand" onclick="dealclick('b1','list');">-</span>
list<ul id="list">
<li>item1</li>
</ul>
<script language="javascript">
document.write(document.getElementById('b1').innerText=='-');
document.write(b1.innerHTML=='-');
</script>
</body>
</html>
/*这样就实现了你的效果*/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
<!--
function dealclick(id1,id2){
alert("1");
if (document.getElementById(id1).innerText =='-'){
alert("2");
document.getElementById(id1).innerText='+';
document.getElementById(id2).style.display='none';
}else{
alert("3");
document.getElementById(id1).innerText='-';
document.getElementById(id2).style.display='block';
}
}
function test(){
alert("调用了javascript");
}
//-->
</script>
</head><body>
<span id="b1" style="border:thin ridge;cursor:hand" onclick="dealclick('b1','list');">-</span>
list<ul id="list">
<li >item1</li>
</ul>
<script language="javascript">
document.write(document.getElementById('b1').innerText=='-');
document.write(b1.innerHTML=='-');
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script language="javascript">
<!--
function dealclick(id1,id2){
//alert(document.getElementById(id1).innerHTML);
if (document.getElementById(id1).innerHTML =='-'){ document.getElementById('b1').innerHTML='+';
document.getElementById(id2).style.display='none';}
else{
document.getElementById(id1).innerHTML='-';
document.getElementById(id2).style.display='block';}
}
function test(){
document.write("调用了javascript");
}
//-->
</script>
</head><body>
<span id="b1" style="border:thin ridge;cursor:hand" onclick="dealclick('b1','list');">-</span>
list<ul id="list">
<li>item1</li>
</ul>
<script language="javascript">
document.write(document.getElementById('b1').innerText=='-');
document.write(b1.innerHTML=='-');
</script>
</body>
</html>调试的时候不要用write,用alert()
if (document.getElementById(id1).innerText =='-'){
document.getElementById(id1).innerText='+';
document.getElementById(id2).style.display='none';}
else{
document.getElementById(id1).innerText='-';
document.getElementById(id2).style.display='block';}
}
不要使用document.write,它会清除掉页面内容;还有'id1','id2'打了引号就是字符串 不是变量了