次のコードが与えられます:
<!DOCTYPE html> <!-- HTML5 -->
<html>
<head>
<meta http-equiv="Expires" content="Fri, Jan 01 1900 00:00:00 GMT">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="content-language" content="en">
<title>Untitled</title>
<script type='text/javascript'>
function createTableRow() {
var tr = document.createElement("tr");
var strWork = "<tr onmouseover='this.style.cursor=\"pointer\";'";
strWork += " onmouseout='this.style.cursor=\"auto\";'";
strWork += " onclick='ShowClick(this);'";
strWork += " id='TestCell'><td>Test!</td></tr>";
alert(strWork);
tr.outerHTML = strWork;
return tr;
}
function BuildTable() {
var table = document.createElement("table");
table.appendChild(createTableRow());
return table;
}
function ShowClick(obj) {
alert(obj.id);
}
</script>
</head>
<body onload='alert(BuildTable().outerHTML);'>
</body>
</html>
最初の「アラート」ダイアログには、形成したいタグが表示されますが、「onload = alert」行は、結果「<table> <tr> </ tr></table>」を返します。
私は何が間違っているのですか?これらのイベントがTRタグにバインドされないのはなぜですか?
また、私が最初にjavascriptを使用してイベントを割り当てたことを追加したいと思います。
tr.onclick = function() { ShowClick(this); };
しかし、まったく同じ結果が得られました...
私は本当にそれを理解していません、そして私はどこにも問題についての議論を見つけることができません...どんな助けも大いにありがたいです!