jQuery .live(“click”, fn) 仅在第二次单击时有效
我有一个用js生成的DOM元素,因此当我想绑定点击事件侦听器时,我需要使用 $( generatedEl).live("click", fn...)
(有不同的方法吗?)
这是我正在使用的代码:(
$(".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
}
});
这是在 .each()
的中间)
但该函数仅在第二次单击时运行。
有人可以帮我找出这个奇怪的错误吗 谢谢
编辑:添加了整个代码块。
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()
}
});
}
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", fn...)
(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()
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.live()
的伟大之处在于它不需要被多次调用。您所要做的就是将其从.each()
中取出。由于您使用类作为选择器,因此您使用该类创建的任何元素都将自动绑定到单击事件。
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.
看来需要从数据中提取变量“isTrunced”。由于它最初没有定义(在 live 函数内),因此默认为 false。
因此,一旦将 live 函数从each 循环中拉出,请尝试以下操作:
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:
问题
好的,我遇到了if 的逻辑有问题的
。这是更正后的:
无论如何,感谢 Ariel,好点。
OK, i got the problem
the logic of the if was buggy.
This is the corrected:
Anyway thanks to Ariel, good point.