两种写法,有种不行,不知道为什么?

polyval01 2010-08-02 02:36:39
//第一种写法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html140/strict.dtd">
<html>
<head>
<title>Sample</title>
<script>
function test1(){
document.getElementById("MyTest").innerHTML=document.getElementById("userText").value;
}
</script>
</head>
<body>
<br>
<h1 id="MyTest" align="center">My Test String!<h1><br>
<form name="MyTestForm" id="MyTestForm">
<input type="button" value="左对齐" onclick="document.all['MyTest'].align='left'">
<input type="button" value="右对其" onclick="document.all['MyTest'].align='right'">
<input type="button" value="中间对齐" onclick="document.all['MyTest'].align='center'"><br>
<input type="button" value="字体红色" onclick="document.all['MyTest'].style.color='red'">
<input type="button" value="字体绿色" onclick="document.all['MyTest'].style.color='green'">
<input type="button" value="字体黑色" onclick="document.all['MyTest'].style.color='black'"><br>
<input type="text" name="用户内容" id="userText" size="30">
<input type="button" value="改变字符串内容"
onclick="test1()"><br>
</form>
</body>
</html>



//第二种写法不行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html140/strict.dtd">
<html>
<head>
<title>Sample</title>
</head>
<body>
<br>
<h1 id="MyTest" align="center">My Test String!<h1><br>
<form name="MyTestForm" id="MyTestForm">
<input type="button" value="左对齐" onclick="document.all['MyTest'].align='left'">
<input type="button" value="右对其" onclick="document.all['MyTest'].align='right'">
<input type="button" value="中间对齐" onclick="document.all['MyTest'].align='center'"><br>
<input type="button" value="字体红色" onclick="document.all['MyTest'].style.color='red'">
<input type="button" value="字体绿色" onclick="document.all['MyTest'].style.color='green'">
<input type="button" value="字体黑色" onclick="document.all['MyTest'].style.color='black'"><br>
<input type="text" name="用户内容" id="userText" size="30">
<input type="button" value="改变字符串内容"
onclick="document.getElementById("MyTest").innerHTML=document.getElementById("userText").value"><br>
</form>
</body>
</html>
...全文
58 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a124819202 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zswang 的回复:]
双引号冲突


HTML code
onclick="document.getElementById('MyTest').innerHTML=document.getElementById('userText').value"
[/Quote]
是这样的
王集鹄 2010-08-02
  • 打赏
  • 举报
回复
或者
onclick="document.getElementById("MyTest").innerHTML=document.getElementById("userText").value"
王集鹄 2010-08-02
  • 打赏
  • 举报
回复
双引号冲突

onclick="document.getElementById('MyTest').innerHTML=document.getElementById('userText').value"
polyval01 2010-08-02
  • 打赏
  • 举报
回复
哎,发帖没注意。
APM60- 2010-08-02
  • 打赏
  • 举报
回复
双引号~

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧