/ / Problem z innerXhtml i atrybutem onclick - javascript, xhtml, innerxhtml

Problem z atrybutami innerXhtml i onclick - javascript, xhtml, innerxhtml

Na stronie XHTML mam ten div:

<div id="listAzioni">
<h4 style="margin-bottom:3px;">List shape :</h4>
</div>

Następnie włączony do div element wejściowy za pomocą javascript przy użyciu biblioteki innerXhtml.

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

Ale po dodaniu element wejściowy nie ma atrybutu onclick. Tak bardzo się starałem, ale nie znalazłem niczego, co rozwiązałoby mój problem. Gdzie robię źle?

Odpowiedzi:

0 dla odpowiedzi № 1

Które przeglądarki wypróbowałeś?

Sugeruję używanie standardowych interfejsów API DOM, piszę kod w następujący sposób:

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