2016-04-26 7 views
0

私はLoL(legends of legends)apiでアプリケーションを作成しています。
現在、私は自分の入力に問題があります。最初に私はinputboxsubmitbuttonをJavaScriptで作成し、コードを書いて使いました。HTMLとjavascriptのコラボ

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); 
}); 

私の質問はどのように私のjsファイルの代わりに私のindex.htmlに置くことができます。 フォームを使用して1ページのアプリケーションにすることはできません。

ありがとうございます。

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