Javascript选择要调用的脚本

时间:2014-04-06 02:48:11

标签: javascript

<!DOCTYPE html>
<html>
<head>
<style>
    #container { 
    width: 320px; 
    height: 400px;
    border: 1px solid red;

    }
    #top { 
    width: 320px; 
    height: 80px;
    border: 1px solid blue;

    }

</style>
<script>
var type;
function one(){
    type = One.js
}
function two(){
    type = Two.js
}


</script>

</head>
<body>

<div id="top">
<button onclick="one()">One</button>
<button onclick="two()">Two</button>

<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
<button onclick="pause()">Pause</button>

</div>


<div style="position: relative;" id="container">

 <canvas id="myCanvas" width="320" height="400" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
       <canvas id="trail" width="320" height="400" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>

    <canvas id="plane" width="320" height="400" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
    <canvas id="buildings" width="320" height="400" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
       <canvas id="cloud" width="320" height="400" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>

    <canvas id="buildings2" width="320" height="400" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>

 <canvas id="canvas2" width="100" height="100" 
   style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>



<script type="text/javascript" src=type> </script>

</body>
</html>

是否可以在激活脚本之前使用按钮来定义正在播放的脚本。因此,需要一个下拉框来编辑正在激活的脚本。我不知道如何做到这一点。 谢谢:))

2 个答案:

答案 0 :(得分:1)

您可以将值传递给函数以选择要使用的值。例如:

function select(which){
    switch(which)
  {
    case 1:
    // execute One.js
    break;
    case 2:
    // execute Two.js
    break;
    default:
    //
  }

}

答案 1 :(得分:1)

我认为这就是你正在寻找的东西:

<body onload="load(0);">

<button onclick="load(0);">One</button>
<button onclick="load(1);">Two</button>

<script>
var myScripts = [
  "./js/scriptOne.js", "./js/scriptTwo.js"
];

function load(i){
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", myScripts[i]);
  document.getElementsByTagName("head")[0].appendChild( fileref );
}
</script>

这样您就可以动态加载脚本。