为什么这个函数javascript没有提醒?
首先,点击CLICK 1
它会显示delete
,然后点击delete
为什么不提醒111-aaaa
我该怎么做?
https://jsfiddle.net/tdpusq05/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div onclick="test_fn1()">CLICK 1</div>
<div id="demo"></div>
<script>
function test_fn1() {
document.getElementById("demo").innerHTML = "<span onclick='delete_fn('111-aaaa')'>delete</span>";
};
</script>
<script>
function delete_fn(no_delete)
{
alert(no_delete);
};
</script>
答案 0 :(得分:1)
从onclick
周围的delete_fn('111-aaaa')
删除单引号,如下所示
function test_fn1() {
document.getElementById("demo").innerHTML = "<span onclick=delete_fn('111-aaaa')>delete</span>";
};
答案 1 :(得分:0)
更改elif
方法以转义引号,此处为fiddle
test_fn1
答案 2 :(得分:0)
<html>
<head>
</head>
<body>
<div onclick="test_fn1()">CLICK 1</div>
<div id="demo"></div>
<script>
function test_fn1() {
document.getElementById("demo").innerHTML = "<span onclick='delete_fn(\"111-aaaa\")'>delete</span>";
}
</script>
<script>
function delete_fn(no_delete) {
alert(no_delete);
};
</script>
</body>
</html>
答案 3 :(得分:0)
JS不会通过使用onclick函数捕获动态创建的元素。 你必须做event delegation(如果你使用 jQuery ): -
$('#id').on('click', 'yourSelector', function() {
//do something
});