2016-10-24 8 views
-5

私はノードjsを使用していたWebサイトで作業しています。 jsファイルに関数を追加しました。今、私がHTMLページから関数を呼び出そうとしているとき、その関数は呼び出されていません。jsファイルの私の関数がhtmlファイルから呼び出されていません

getSpeechBubble: function() { 

     console.log(' getSpeechBubble '); 
} 
+0

これはどのように呼びますか?また、コードを適切にフォーマットしてください。 –

+0

どのように関数を呼び出していますか?どのようにJSファイルを含めるのですか?キャッシュしていないと確信していますか?コンソールにエラーがありますか?あなたはそれをデバッグしようとしましたか? – vlaz

+0

HTMLは有効ではなく、JSは宣言方法に依存しません – Li357

答えて

0

関数を作成して呼び出すには、いくつかの方法があり、最も人気のある使用:

<div id ="bubble" class="bubble-style" data-action="getSpeechBubble()</div> 

は私だけの機能でコンソールのステートメントを追加しました: これは私がHTMLページで使用されるコードであります

function getSpeechBubble(){ 
 
console.log("getSpeechBubble"); 
 
} 
 
//get function from data-action attribute 
 
var exc = document.getElementById("bubble").getAttribute("data-action"); 
 

 
//or window.addEventListener("load",new Function(exc),false); 
 
window.onload = new Function(exc); 
 
//or .addEventListener("clik",new Function(exc),false); 
 
document.getElementById("bubble").onclick = new Function(exc);
<div id ="bubble" class="bubble-style" data-action="getSpeechBubble()"> 
 
click me 
 
</div>

を下回っています10
//1st way 
this.getSpeechBubble = function() { 
      console.log(' 1st way: getSpeechBubble '); 
} 
this.getSpeechBubble(); 
//2nd way 
this["getSpeechBubble"] = function() { 
      console.log(' 2nd way: getSpeechBubble '); 
} 
this["getSpeechBubble"](); 
//3rd way 
window["getSpeechBubble"] = function() { 
      console.log(' 3rd way: getSpeechBubble '); 
} 
window["getSpeechBubble"]() 
//4th way 
var getSpeechBubble = function() { 
      console.log(' 4th way: getSpeechBubble '); 
} 
getSpeechBubble(); 
//5th way 
var js = { 
getSpeechBubble: function(){ 
      console.log(' 5th way: getSpeechBubble '); 
} 
}; 
js.getSpeechBubble(); 
//6th way 
function getSpeechBubble() { 
      console.log(' 6th way: getSpeechBubble '); 
} 
getSpeechBubble(); 
関連する問題