1
我有哪些即时猜测是发生点击事件的一些问题,因为什么,我点击这里在页面加载后得到追加是jQuery和的jsfiddlejQuery的具有附加列表项
click事件问题$(document).ready(function(){
// Adding a project
$('.project-btn').click(function(e){
e.preventDefault();
//grab the user input for the new project
var project = $('.project-val').val();
//Add the project to the list
$('<li></li>').addClass(project).appendTo('.project-list');
$('<a></a>').attr("href",project).text(project).appendTo('li.'+project);
// create the div where the categories will go
$('<div></div>').attr("id",project).appendTo('.category-wrapper');
// hide the div untill it is called upon
$('div#'+project).fadeOut(function(){
$('<h1></h1>').text(project).css("text-align","center").appendTo('div#'+project);
// add the input for the category div
$('<input>').attr("type","text").addClass('category-val').appendTo('div#'+project);
$('<input>').attr("type","submit").attr("value","Add Category").addClass("category-btn-"+project).appendTo('div#'+project);
// add the ul
$('<ul></ul>').attr("class","category-list").appendTo('div#'+project);
// add the back button
$('<p></p>').text("back").addClass('category-back').css("cursor","pointer").appendTo('div#'+project);
});
// clear the input search
$('.project-val').val('');
});
$('a').click(function(e){
e.preventDefault();
selectedDiv = $(this).attr("href");
alert("clicked");
$('.project-wrapper').fadeOut(function(){
$('div#'+selectedDiv).fadeIn();
});
});
});
我想显示隐藏的div只有当我点击添加列表项。由于某种原因,我说我的猜测以上它不起作用。我添加了一个“单击”警报,以便单击任何锚点元素时,但我无法做出响应
谢谢为什么我必须在动态元素上使用“on”? – swsa
因为你的元素在第一次启动时不存在,所以我可以尽快给你支票 – swsa
@swsa。您的事件处理程序未分配给任何内容,因此在您的元素动态添加时不存在。在使用'on()'时,我们将事件处理程序添加到'body'中,该元素在添加元素之前存在。 –