JavaScript not running on jsfiddle.net

后端 未结 4 1077
无人共我
无人共我 2020-11-22 09:42

The code below works on a live site but I can\'t get it to run on the site jsfiddle .

See this for example.

Can anyone tell me why it\'s not working on jsfid

4条回答
  •  情话喂你
    2020-11-22 10:04

    The functions you define are defined in an onload function, so whereas before they were referenceable, because they are defined in that function they can only be referenced from within that function. You reference them as globals in your HTML. You have three options

    a) ( easiest, quickest, not ideal ) - change function blah(){} to window.blah = function(){}; making the functions global.

    b) ( ideal way ) - use unobtrusive Javascript to attach behaviour to DOM elements from within the JS solely, meaning separate HTML from JS.

    c) Make the jsfiddle not wrap the stuff onload. Change onLoad to no wrap ( body or head ).

    So instead of

    you'd do var e = document.getElementById('foo'); e.onclick = lol; in the JS only.

    I recommend b as it encourages best practices.

提交回复
热议问题