2016-04-26 47 views
0

我正在用LoL(英雄联盟)API进行应用程序。
目前我在输入时遇到问题。首先,我在JavaScript中创建了一个inputboxsubmitbutton,并编写了一些代码,以便我可以使用它。HTML and javascript colab

var inputbox = document.createElement("input"); 
inputbox.type = "input"; 
document.body.appendChild(inputbox); 

var button = document.createElement("input") 
button.type = "button"; 
button.value = "Press me!"; 
document.body.appendChild(button); 

之后我用这个代码来使用输入值。

button.addEventListener("click", function() { 
    LookUpSummonerInformation(inputbox.value); 
}); 

我的问题是,我怎么能把这个到我的index.html而不是我的js文件。 我不能使用表单来完成它,因为它可能是单页面应用程序。

在此先感谢。

+0

标签之间的代码! –

+0

为什么不直接写html?你可以把js代码放在'' – neilsimp1

回答

3
<!DOCTYPE html> 
<html> 
    <head> 
    <title>LoL Thing</title> 
    </head> 

    <body> 
    <input type='input' id='lol-input'></input> 
    <button onclick='do_the_thing();'>Press me!</button> 

    <script> 
     function do_the_thing() { 
     var inputbox = document.querySelector('#lol-input'); 
     LookUpSummonerInformation(inputbox.value); 
     } 
    </script> 
    </body> 
</html> 

这里是你将如何与HTML混合JS一个简单的例子。

+0

这似乎是一个好主意,但是我怎么能包含我的脚本文件(lol。 JS)如果脚本标记已被使用? –

+0

编辑:测试了这一点,我仍然可以包含该文件。 –

1

我猜你正在寻找这样的事情

<html> 
    <head> 
    <script> 
     document.onload = function(){ 
     var inputbox = document.createElement("input"); 
     inputbox.type = "input"; 
     document.body.appendChild(inputbox); 

     var button = document.createElement("input") 
     button.type = "button"; 
     button.value = "Press me!"; 
     button.addEventListener("click", function() { 
      LookUpSummonerInformation(inputbox.value); 
     }); 
     document.body.appendChild(button); 
     } 
    </script> 
    </head> 
    <body> 
    ... 
    </body> 
</html> 
0

您可以随时在您的html文档正文中使用<script>标签包含JavaScript代码。

如:

<html> 
    <head> 
    <!-- Head code here --> 
    </head> 
    <body> 
    <script> 
     // Your JavaScript code goes here 
    </script> 
    <!-- Remaining body code here --> 
    </body> 
<html> 

或者

您还可以使用<script>标签链接的javascript到你的索引文件。

如:

<html> 
    <head> 
    <!-- Head code here --> 
    </head> 
    <body> 
    <script src="path_to_your_js_file"></script> 
    <!-- Remaining body code here --> 
    </body> 
<html>