gpt4 book ai didi

javascript - 单击事件不会在新创建的 li 标签上触发

转载 作者:行者123 更新时间:2023-11-28 04:57:42 24 4
gpt4 key购买 nike

使用以下代码:

<!DOCTYPE html>
<html>
<head>
<title>test list</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
</head>
<style>
li{
display:inline;
}
</style>
<body>
<input type="hidden" value="4" id="value">

<ol></ol>

<button id="btn2">increase</button>
<button id="btn1">show</button>
<p></p>
</body>
<script>
$("li").click(function(){
$(this).nextAll().css({"color":"red"});;
});

$("#btn2").click(function(){
var text="<li> -> kkk</li>";
$("ol").append(text);
});

$("#btn1").click(function(){
$("p").text($("li").length);
});
</script>
</html>

点击“增加”按钮后出现的任何新创建的“li”标签,都不会触发绑定(bind)到点击事件的处理程序。

$("li").click(function(){
$(this).nextAll().css({"color":"red"});;
});

你能告诉我它不起作用的原因吗?有可能让它发挥作用吗?如果是,如何?非常感谢。

最佳答案

像这样尝试:当你的“li”动态生成时(For further reading)

$("body").on('click','li',function(){
$(this).nextAll().css({"color":"red"});;
});

关于javascript - 单击事件不会在新创建的 li 标签上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20087738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com