-2

jsでこの関数を使用できないのはなぜですか:エラー:ReferenceError:new_tweetが定義されていません

  function new_tweet(){
         alert("here i am");
  }

  function add_div(){
    mydiv = document.getElementById("new_twt");
    mydiv.innerHTML="<input type='button' value='You have new conversations' onclick=new_tweet();>";
  }
4

3 に答える 3

2

問題はおそらく、ある種のロードハンドラーでこれを実行していることです。これは、new_tweetグローバルではないため、ボタンをクリックしたときに使用できないことを意味します。new_tweetグローバルスコープで定義してみてください。

これがデモンストレーションです:http://jsfiddle.net/DUbc3/

于 2012-12-05T13:44:45.273 に答える
1

onclickに'を追加してみてください

<input type='button' value='You have new conversations' onclick='new_tweet()'>
于 2012-12-05T13:43:30.043 に答える
1
var newTweet = function (e) {
     alert("here i am");
}

function add_div(){
    var button = document.createElement("button");
    button.innerHTML = 'You have new conversations';
    button.onclick = newTweet;
    document.getElementById("new_twt").appendChild(button);
}

add_div();

デモを参照してください:http://jsfiddle.net/DUbc3/1/

于 2012-12-05T13:46:17.813 に答える