当展开点击 li 的 ul 事件时,JS JQUERY 错误不起作用
JS JQUERY bug when expanding ul event of click on li doesnt work
我创建了一个简单的网页来学习 JQuery,但是每当我点击按钮时:另一个它使点击 li 的事件消失,出现故障,当点击 li 时它什么也没做......这是代码:JS:
$(document).ready(function(){
console.log("Ready");
var toggle = 0;
$("li").on("click",function(){
console.log("Clicked on li");
try{
$(this).hide(800);
$(this).removeClass("con");
$(this).addClass("con");
}
catch(err){
console.log("Exception "+err.message);
}
});
$("input").on("focus",function(){
console.log("Focused on input");
$("li").hide(800);
$("#D").html("Open div");
toggle = 1;
});
$("input").on("focusout",function(){
console.log("FocusedOut on input");
$("li").html($("input").val());
$("li").show(500,function(){
$("#D").html("Close div");
toggle = 0;
});
});
$("button").on("click",function(){
console.log("Clicked button :"+$(this).html());
if($(this).attr("id")=="s"){
$("li").show(300);
}
else if($(this).attr("id")=="n"){
$("li").removeClass("con");
}
else if($(this).attr("id")=="C"){
$("li").addClass("con");
}
else if($(this).attr("id")=="an"){
console.log("Added to html : "+$(".uDefined").html()+"<br/><li>"+$("li").last().html().substring(0,7)+($("li").last().html().substring(7,8)-(1)+1+1)+"</li>");
$(".uDefined").html($(".uDefined").html()+"<br/><li>"+$("li").last().html().substring(0,7)+($("li").last().html().substring(7,8)-(1)+1+1)+"</li>");
}
else{
$(".list").slideToggle(1000,function(){
if(toggle==0){
$("#D").html("Open div");
toggle=1;
}
else{
$("#D").html("Close div");
toggle = 0;
}
});
}
});
}(;
.HTML:
<html>
<style type="text/css">
.list{
font-size: 100;
background-color: white;
}
li{
margin-left: 100;
border-radius: 20;
border-width: 3;
background-color: Black;
color:White;
}
.con{
color:red;
}
.uDefined{
}
</style>
<body onload="" style="background-color:gray;margin:40" >
<title>JQuery TryOut</title>
<h3 style="margin-left:800;">Welcome</h3>
<button id="D">Close Div</button>
<button id="s">Show All</button>
<button id="n">Remove Classes</button>
<button id="C">Add Classes</button>
<div class="list">
<ul class="uDefined">
<li class="con">content1</li>
<li>content2</li>
<li>content3</li>
<li>content4</li>
<li>content5</li>
<li>content6</li>
</ul>
</div>
<div>
<p>Please focus on this text box:</p><input id="ttt" placeholder="Enter text here" value="" type="text" />
</div>
<button id="an" >another one</button>
</body>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="JavaScript.js"></script>
感谢帮手!
直接事件仅在调用 .on()
方法时附加到元素(存在(。因此,在动态添加按钮时,需要使用委托的事件处理程序。您应该像这样更改代码:
$("ul").on("click", "li", function () {
...
});
有关委派事件的更多信息:http://learn.jquery.com/events/event-delegation/
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 点击facebook像素跟踪注册(JS/JQUERY代码)
- 用于发现哪个JS/jQuery脚本正在冻结页面的工具或技术
- 使用js/jQuery检查对象(而不是元素)是否真的存在
- 如何使用JS/jQuery在另一个网站上显示iframe中的特定内容
- 在坐标上模拟点击坐标js/jQuery
- 如何插入js/jquery变量isideHTML标记
- 我需要JS/jQuery中的倒计时计时器
- 当我使用JS/jQuery/any插件点击图像时,我如何使背景变暗+不可点击,并放大和居中我的图像
- JS/jquery:我将如何遍历HTML中的每个图像
- 非常奇怪的JS/jQuery行为-alert()包含/排除
- JS/Jquery - 如果输入具有数组的任何名称,则返回关联的值
- 使用 js/jQuery/css 在重力形式中覆盖 !important
- 如何在 js / jquery 中绑定“数据追加”事件
- 解析 RSS 命名空间媒体:缩略图 (js/jQuery)
- AJAX/page refresh kills JS/jQuery
- 带有post值的JS jQuery重定向,不带<表单>
- 改变与“"至“-"(JS/Jquery)
- 我正在尝试使用JS/JQuery自动对网站中的文本执行多个查找和替换操作
- 是否覆盖网站上文本输入区域的默认移动设备键盘?(HTML/JS/jQuery)