我正在尝试让我的 cloneNode
附加 input
标记并为其赋予新的 onclick
值。只是 input.onclick = function(){clicker(iden);};
不起作用。此示例的唯一目的是更改 onclick 标记,就像我设法更改 ID 一样。有人可以帮忙吗?
脚本
<script>
iden = 0
function clicker(x) {
iden++
var ent = document.getElementById('ent').cloneNode(true);
ent.id = "ent"+iden;
var input = ent.getElementsByTagName('input')[0];
input.id = "inp"+iden;
input.onclick = function(){clicker(iden);};
document.body.appendChild(ent);
}
</script>
正文
<body>
<div id="ent">
<input id="inp" onclick="clicker()">
</div>
<body>
最佳答案
删除
function () {
在你的标签底部为我修复了它
<html>
<body>
<script>
iden = 0
function clicker(x) {
iden++
var ent = document.getElementById('ent').cloneNode(true);
ent.id = "ent"+iden;
var input = ent.getElementsByTagName('input')[0];
input.id = "inp"+iden;
input.onclick = function(){
clicker(iden);
};
document.body.appendChild(ent);
}
</script>
<div id="ent">
<input id="inp" onclick="clicker()">
</div>
</body>
</html>
关于javascript - 如何 'getelementsbytagname.onclick',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168220/