更新您的小提琴以使用no wrap - in <head>
orno wrap in <body>
或 jsFiddle 将默认情况下将脚本包装到window.onload
. 这会导致元素首先呈现,并且在onclick
呈现时脚本还不存在,因为它仅在加载窗口后才被添加,如下所示:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>- jsFiddle demo</title>
<script type="text/javascript" src="/js/lib/dummy.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type="text/css">
</style>
<script type="text/javascript">
//<![CDATA[
window.onload = function() {
function test() {
alert("TEST");
}
function arrayOfOandZ(size) {
var arrayOFOnZ = new Array(size);
//Fill array
for (var x = 0; x < size; x++) {
arrayOfOnZ[x] = size % 2;
}
alert(arrayOFOnZ.join('\n'));
}
} //]]>
</script>
</head>
<body>
<input type="button" value="5 Items" onclick="test()">
<input type="button" value="6 Items" onclick="arrayOfOandZ(6)">
<input type="button" value="7 Items" onclick="arrayOfOandZ(7)">
</body>
</html>
演示- 小提琴中的固定设置
no wrap in <head>
例如,将脚本直接呈现到 中,head
而无需window.onload
包装器使其立即可用。因此,当元素被渲染时,脚本存在,如下所示:
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>- jsFiddle demo</title>
<script type="text/javascript" src="/js/lib/dummy.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type="text/css">
</style>
<script type="text/javascript">
//<![CDATA[
function test() {
alert("TEST");
}
function arrayOfOandZ(size) {
var arrayOFOnZ = new Array(size);
//Fill array
for (var x = 0; x < size; x++) {
arrayOfOnZ[x] = size % 2;
}
alert(arrayOFOnZ.join('\n'));
}
//]]>
</script>
</head>