jQuery .live("click", fn) works only on second click
I have a DOM element that is generated with js, and therefore when i want to bind a click event listener i need to use $(generatedEl).live("click", f开发者_如何学Cn...)
(is there a different way?)
here is the code i am using:
$(".toggleView").live("click", function(){
if(isTrunced){
$(this).html(cntarctText).siblings("h3").html(currEl.data("md").myFullText)
isTrunced = false
}
else{
$(this).html(expandText).siblings("h3").html(currEl.data("md").truncedText)
isTrunced = true
}
});
(this is in middle of a .each()
)
But the function only runs on the second click.
can someone please help me track down this weird bug, Thanks
EDIT: Added the entire code block.
var truncMe = function(passedNode, passedChanges){
var truncTarget = passedNode,
expandText = "more",
cntarctText = "less",
isTooLong = false,
isTrunced = false,
maxChar = 170,
toggleView
truncTarget.each(function (index, domEle) {
var currEl = $(domEle)
currEl.data("md", {myFullText:currEl.html(),isTooLong:false, isTrunced:false })
if(currEl.data("md").myFullText.length >= maxChar){
currEl.data("md").truncedText = currEl.data("md").myFullText.substring(0, maxChar);
currEl.data("md").isTooLong = true;
currEl.siblings(".toggleView").remove()
if(passedChanges){
currEl.data("md").myFullText = passedChanges;
currEl.data("md").truncedText = currEl.data("md").myFullText.substring(0, maxChar);
}
/* here the element is created */
toggleView = $("<div class='toggleView'/>").html(expandText).appendTo(currEl.parent());
currEl.html(currEl.data("md").truncedText)
/* here the event is binded */
$(".toggleView").live("click", function(){
if(isTrunced){
$(this).html(cntarctText).siblings("h3").html(currEl.data("md").myFullText)
isTrunced = false
}
else{
$(this).html(expandText).siblings("h3").html(currEl.data("md").truncedText)
isTrunced = true
}
});
}
else{
currEl.siblings(".toggleView").remove()
}
});
}
The great thing about .live()
is that it doesnt need to be called more than once. All you have to do is take it out of the .each()
.
Since you are using a class as a selector any element that you will create with that class will automatically be bound to the click event.
It appears that the variable "isTrunced" needs to be extracted from the data. Since it isn't defined initally (inside the live function) it will default to false.
So once you pull the live function out of the each loop, try this:
$(".toggleView").live("click", function(){
if($(this).data("isTrunced")){
$(this).html(cntarctText).siblings("h3").html(currEl.data("md").myFullText)
$(this).data("isTrunced", "false");
} else {
$(this).html(expandText).siblings("h3").html(currEl.data("md").truncedText)
$(this).data("isTrunced", "true");
}
});
OK, i got the problem
the logic of the if was buggy.
This is the corrected:
if(isTrunced){
$(this).html(cntarctText).siblings("h3").html(currEl.data("md").myFullText)
isTrunced = false
}
else{
$(this).html(expandText).siblings("h3").html(currEl.data("md").truncedText)
isTrunced = true
}
Anyway thanks to Ariel, good point.
精彩评论