Trying to log to console with onclick event [closed]

半世苍凉 提交于 2019-12-05 08:20:36

问题


Trying to make a div respond to click events.

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/

edit: Had forgotten to update the jsfiddle


回答1:


For an inline click event, this is what you need:

<div onclick='console.log("Hello")'>1</div>

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>

var test = document.getElementById('test');
test.onclick = function() {
    console.log('Hello');
}

http://jsfiddle.net/vu3xo3am/3/



来源:https://stackoverflow.com/questions/26236196/trying-to-log-to-console-with-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!