动态添加的链接(classnames为
.divToggle
和 .removeDiv
)只有在第一次点击两次时才能正常工作。是什么阻止了它们立即正常工作?$(document).ready(function(){
// adds click event to links.
$('a.divToggle').live('click', function(event) {
// Toggles the visibility of divs.
event.preventDefault;
$(this).toggle(function(){
$(this).next(".divToToggle").slideUp("slow");
$(this).text("show[+]");
},
function(){
$(this).next(".divToToggle").slideDown("slow");
$(this).text("hide[-]");
});
});
// used to remove divs from the page.
$("a.removeDiv").live("click", function(event) {
event.preventDefault;
$(this).parent().prev("a").prev("h2").remove();
$(this).parent().prev("a").remove();
$(this).parent().next("br").remove();
$(this).parent().remove();
});
// Used to add new divs to the page.
$(".addDiv").click(function(){
$("<h2>Title Area</h2><a href='#' class='divToggle'>hide[-]</a>"
+ "<div class='divToToggle'><a href='#' class='removeDiv'>Remove this div</a>"
+ "<ul><li>List element 1</li><li>List element 2</li>"
+ "<li>List element 3</li></ul></div><br />").insertBefore($(this));
});
});