I have created a JSFiddle to describe the issue. Why isn't the alert displayed when I call the doSomething()
function?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Your function
dosomeThing()
is not defined in the page just replace header tag with this oneand then try again
You need to disable framework option in jsfiddle for javascript to work
DEMO
This is because
doSomething()
function is not defined in the HTML page. In jsfiddle your function (in js pane) is wrapped by document onload event of jquery. (see at the left side of jsfiddle for settings). So Its executed like this,See how its wrapped. Its not accessible. To fix it you should assign it to
window
object. In the js pane write the function asIt'll work
Its recommended you do not use
onclick
attributes of an HTML elements. Its better to assign event handler with JS.What JsFiddle does for you is creating the
<HTML>
,<head>
and<body>
tags for you. You shouldn't include them in your HTML, because that would make the markup invalid after JsFiddle processed it. It also wraps your JS in the document's onload event. So your function was not defined in the root scope as you thought, but in th scope of thedocument.onload
function, and you couldn't reach it from within the body, because that is outside of that function. I changed the JsFiddle attribute 'wrap in' to 'no wrap (head)' and it worked.This is a "fiddle-thing". Pick
nowrap (head)
from the first selection in the "Choose Framework" field.Here is your complete code. just copy and paste your editor. it is