JQuery预先崩溃 [英] JQuery prepend to crash
问题描述
如果我ajax任何输入到我的页面下面的代码工作,但如果我打印一些特定的代码,我得到一个异常,警告test3从未启动。
alert(test2);
$(html).hide()。prependTo(#current table.vtable)。fadeIn(slow);
alert(test3);
我得到jquery异常:
未捕获类型错误:无法设置未定义的属性'cur'
我已经说过CSS是工作正常,我试过打印其他类,它的工作完全唯一的区别是我使用
print<<< END
...
END;
而不是普通打印,而且tr是不同的类
从jquery 1.7升级到1.9后,我遇到了这个问题。这是在fadeIn()的实现中的一个错误。
它与data()方法现在返回未定义,而不是null当你查询一个不存在的键。
无论如何,我发现的解决方法是先添加数据,然后获取一个jquery句柄到新的前缀内容,
由于您要将新内容插入表格中,因此您可能会发现您刚刚插入的内容,因为它实际上不是第一个孩子(它被包裹在一个tbody标签隐式)。我通过在插入的东西上设置临时属性,搜索它,然后删除它来处理 。这是一个巨大的黑客明显,但它 工作。
var html =< tr> ;< td> new thing1< / td>< td> new thing2< / td>;
$(#clickme)。click(function(){
var target = $(#current table.vtable);
$(html).attr (jquery_workaround,sigh)。hide()。prependTo(target);
var newstuff = target.find([jquery_workaround = sigh]);
newstuff.removeAttr(jquery_workaround ).fadeIn(slow);
}); http://jsfiddle.net/BUVUj/
If I ajax any input into my page the following code works but if I print some specific code I get an exception, alert test3 never fires.
alert("test2");
$(html).hide().prependTo("#current table.vtable").fadeIn("slow");
alert("test3");
I get the jquery exception:
Uncaught TypeError: Cannot set property 'cur' of undefined
As I've said the CSS is working proper, I have tried printing other classes and it works perfectly the only difference is I use
print <<<END
...
END;
Instead of normal print and the tr is a different class
解决方案 I just ran into this issue after upgrading from jquery 1.7 to 1.9. This is a bug in the implementation of fadeIn().
It has to do with the fact that the data() method now returns undefined rather than null when you query for a key that doesn't exist.
Anyhow, a workaround that I found is to prepend the data first, and then get a jquery handle to the new, prepended content, and fade that in.
Since you're inserting the new content into a table, it's a little annoying to find the thing you just inserted, since it's not actually the first child (it's wrapped in a "tbody" tag implicitly). I worked around this by setting a temporary attribute on the inserted thing, searching for it, and then removing it. This is a gigantic hack obviously, but it does work.
var html = "<tr><td>new thing1</td><td>new thing2</td>";
$("#clickme").click(function () {
var target = $("#current table.vtable");
$(html).attr("jquery_workaround","sigh").hide().prependTo(target);
var newstuff = target.find("[jquery_workaround=sigh]");
newstuff.removeAttr("jquery_workaround").fadeIn("slow");
});
For the fiddling: http://jsfiddle.net/BUVUj/
这篇关于JQuery预先崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!