我写的这个JS代码,逻辑上有问题吗? 为什么执行不了onclick后的效果

qq_40145758 2017-09-07 06:03:08
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>jsbasicversion8</title>
<script>
window.onload=saySomething;
function saySomething() { switch(this.id

) {
case "trump":
alert("Four score and seven years ago..."); break;
case "Kenidy":
alert("Ask not what your country can do for you..."); break;
case "clinton":
alert("I am not a crook!"); break;
default: }
}
</script>
</head>

<body>
<h2>
THE FAMOUS PRESIDENT QUOTES
</h2>

<input type="button" value="kenidy" id="kenidy" onClick= "saySomething();"/>
<input type="button" value="clinton" id="clinton" onClick="saySomething();"/>
<input type="button" value="trump" id="trump" onClick="saySomething();"/>

</body>
</html>
...全文
1663 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_40145758 2017-09-11
  • 打赏
  • 举报
回复
谢谢大家的帮助
qq_40145758 2017-09-08
  • 打赏
  • 举报
回复
引用 6 楼 cyzclwan的回复:
onclick调用函数 你得传参啊,onclick里面的参数this才是传递的 这个元素本身
this 是指向input 是吗?谢谢您
cyzclwan 2017-09-08
  • 打赏
  • 举报
回复
onclick调用函数 你得传参啊,onclick里面的参数this才是传递的 这个元素本身
qq_40145758 2017-09-08
  • 打赏
  • 举报
回复
引用 2 楼 孟子E章的回复:
这么改
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>jsbasicversion8</title>
<script> 

function saySomething(obj) { 

switch(obj.id) {
case "trump":
alert("Four score and seven years ago..."); break;
case "Kenidy":
alert("Ask not what your country can do for you..."); break;
case "clinton":
alert("I am not a crook!"); break;
default: }
}
</script>
</head>

<body>
<h2>
THE FAMOUS PRESIDENT QUOTES
</h2>

<input type="button" value="kenidy" id="kenidy" onclick= "saySomething(this);"/>
<input type="button" value="clinton" id="clinton" onclick="saySomething(this);"/>
<input type="button" value="trump" id="trump"  onclick="saySomething(this);"/>

</body>
</html>
您好非常感谢! 能再帮解释一下为什么用 参数 this/object 就OK了呢?我有点看不明白!
一枚大帅哥 2017-09-08
  • 打赏
  • 举报
回复
引用 7 楼 qq_40145758 的回复:
[quote=引用 6 楼 cyzclwan的回复:]onclick调用函数 你得传参啊,onclick里面的参数this才是传递的 这个元素本身
this 是指向input 是吗?谢谢您[/quote] 对的。
qq_18868801 2017-09-07
  • 打赏
  • 举报
回复
在标签里面写onclick事件时,触发某个函数,不需要window.onload,直接在script定义一个函数就行了,同时调试下在调用函数时,是否需要传入this参数进去
  • 打赏
  • 举报
回复
你应该使用 vs 断点调试 this.id 属性。
孟子E章 2017-09-07
  • 打赏
  • 举报
回复
这么改
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>jsbasicversion8</title>
<script> 

function saySomething(obj) { 

switch(obj.id) {
case "trump":
alert("Four score and seven years ago..."); break;
case "Kenidy":
alert("Ask not what your country can do for you..."); break;
case "clinton":
alert("I am not a crook!"); break;
default: }
}
</script>
</head>

<body>
<h2>
THE FAMOUS PRESIDENT QUOTES
</h2>

<input type="button" value="kenidy" id="kenidy" onclick= "saySomething(this);"/>
<input type="button" value="clinton" id="clinton" onclick="saySomething(this);"/>
<input type="button" value="trump" id="trump"  onclick="saySomething(this);"/>

</body>
</html>
qq_40145758 2017-09-07
  • 打赏
  • 举报
回复
小农第一次上这个论坛, 不知道在哪里提问,有看到的大牛,帮我回一下为什么 上面我写的代码没有显示ALERT里的内容。 我用F12查了代码,也没找到哪里错误了, 是不是写的逻辑就有问题呢? JAVASCRIPT

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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