Javascript 尝试使用 onclick 事件登录到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26236196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to log to console with onclick event
提问by Moltas
Trying to make a div respond to click events.
试图让 div 响应点击事件。
This is what I've tried:
这是我尝试过的:
<div onclick(console.log("Hello"))>1</div>
<div onclick(console.log(add(2, 5)))>1</div>
<div onClick(function(){ console.log "Hello" })>1</div>
Here is my code: http://jsfiddle.net/kastasten/vu3xo3am/5/
这是我的代码:http: //jsfiddle.net/kastasten/vu3xo3am/5/
edit: Had forgotten to update the jsfiddle
编辑:忘记更新 jsfiddle
回答by wf4
For an inline click event, this is what you need:
对于内联点击事件,这是您需要的:
<div onclick='console.log("Hello")'>1</div>
<div onclick='console.log("Hello")'>1</div>
http://jsfiddle.net/vu3xo3am/2/
http://jsfiddle.net/vu3xo3am/2/
Or,
或者,
If you want to do this unobtrusively, this would be how you could do it:
如果你想不显眼地这样做,你可以这样做:
<div id="test">1</div>
<div id="test">1</div>
var test = document.getElementById('test');
test.onclick = function() {
console.log('Hello');
}

