2011-01-14 9 views
0

属性は、私はこのdiv要素を持っています。innerXhtml通報とonclickのXHTMLページで

var paper = document.getElementById("listAzioni"); 
var toWrite = "<h4 style=\"margin-bottom:3px\">List shape :</h4>"; 
toWrite += "<input type=\"button\" class=\"listButton\" value=\""+id+"\" onclick=\"showAction('"+id+"');\"/>"+'<br/>'; 
innerXHTML(paper,toWrite); 

ただし、input要素を追加した後は、onclick属性はありません。私はとても大変でしたが、私の問題を解決するものは何も見つかりませんでした。どこが間違っていますか?

答えて

0

お試しいただいたブラウザはどれですか?私はあなたが標準DOM APIを使用することをお勧め

、私は以下のようにコードを記述します。

<html> 
    <head> 
    <style type="text/css"> 
    .button { 
     border: 1px solid black; 
     padding: 3px; 
    } 
    </style> 
    <script type='text/javascript'> 
     function addButton() { 
     var container = document.getElementById('container'); 
     if (container != null && container != undefined) { 
      var child = document.createElement("div"); 
      var button = document.createElement("input"); 

      button.value = "Click to alert"; 
      button.type = "button"; 
      child.style.padding = "10px"; 
      child.style.border = "2px solid red";  
      button.className = "button"; 

      button.onclick = showAlert; 
      child.appendChild(button); 
      container.appendChild(child); 
     } 
     } 

     function showAlert() { 
     alert("you clicked the button!"); 
     } 
    </script> 
    </head> 
    <body> 
    <div id="container"></div> 

    <input value="add div and button" type='button' onclick='addButton();'/> 
    </body> 
</html> 
+0

私はFFとChromeで私のコードを試してみましたが、両方は私に、このエラーを与えます。私はあなたから別の提案があると思っていましたが、私は何も変更せずに現在のコードを実行しようとします。 – LuckyStarr

+0

私のコードの結果はどうでしたか?私はいつも標準APIを使用することをお勧めします。JavaScriptがないと思えば、PrototypeやjQueryのような基本的な補完機能をJavaScriptの基本的な機能を使用するいくつかの基本機能で選択できます。 – ehsun7b

+0

..私はあなたのコードを使用し、今は完全に動作します。ありがとう – LuckyStarr