Javascript选择要调用的脚本 #容器{ 宽度:320px; 高度:400px; 边框:1px纯红; } #顶部{ 宽度:320px; 高度:80px; 边框:1px纯蓝色; } var类型; 功能一(){ type=One.js } 函数二(){ type=Two.js } 一个 两个 开始 停止 暂停

Javascript选择要调用的脚本 #容器{ 宽度:320px; 高度:400px; 边框:1px纯红; } #顶部{ 宽度:320px; 高度:80px; 边框:1px纯蓝色; } var类型; 功能一(){ type=One.js } 函数二(){ type=Two.js } 一个 两个 开始 停止 暂停,javascript,Javascript,是否可以在激活脚本之前使用按钮来定义正在播放的脚本。因此,需要一个下拉框来编辑正在激活的脚本。我不知道该怎么做。 谢谢:)您可以向函数传递一个值,以选择要使用的函数。例如: <!DOCTYPE html> <html> <head> <style> #container { width: 320px; height: 400px; border: 1px solid red; } #top {

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

您可以向函数传递一个值,以选择要使用的函数。例如:

<!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>

我想这就是你想要的东西,像这样:

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

}

一个
两个
var myScripts=[
“/js/scriptOne.js”,“/js/scriptTwo.js”
];
功能负荷(一){
var fileref=document.createElement('script');
setAttribute(“类型”、“文本/javascript”);
setAttribute(“src”,myScripts[i]);
document.getElementsByTagName(“head”)[0].appendChild(fileref);
}

通过这种方式,您可以动态加载脚本。

是的,这是可能的,但不是那样的。我该怎么做?未捕获引用错误:未定义文件名是出现的错误。我修复了脚本。现在试试看。
<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>