敲除嵌套绑定会干扰IE7 / IE8中的jquery点击 [英] knockout nested bindings interfere with jquery click in IE7/IE8
问题描述
当我使用jQuery的$ .click()将点击处理程序绑定到一个元素时,它通常工作正常在IE7 / IE8 / IE9。遇到问题的是如果元素(在这种情况下是一个按钮,但它也适用于我的测试中的其他元素类型)在由Knockout绑定的DOM结构内。这只发生在IE7 / IE8中,可惜的是,我在底部分享的jsFiddle链接可能是有限的。在IE9(不出意料之中)行为正常工作。
这似乎也不影响没有jQuery绑定的事件(在纯JavaScript中使用element.attachEvent)。
< div data-bind =with:ContainerObject>
< div data-bind =text:ContainedProperty>< / div>
< input id =alertButtonJQBadtype =buttonvalue =jq / ko = broken/>
< / div>
函数jqClickBad(){alert(由jquery绑定的嵌套click()处理器); }
//这个IE9中的IE7 / IE8不起作用,
$(#alertButtonJQBad)点击(jqClickBad);
var viewModel = {
ContainerObject:{
ContainedProperty:test
}
};
ko.applyBindings(viewModel);
http://jsfiddle.net/csiefken/WckWb/3/
我不能甚至在IE8中运行JS小提琴。 jsFiddle似乎没有正确显示给我。
一般来说,如果你使用常规的jQuery事件绑定而不是KO事件绑定,那么你会反对谷物。如果你真的需要使用jQuery来注册你的处理程序而不是Knockout事件绑定,那么请在 ko.applyBindings
之后注册或使用live event处理程序(通过向 $。on()
提供css选择器),以便您的绑定将在Knockout执行的任何潜在DOM重写中生存。
It looks to me as if there is a bug in Knockout when used in IE7/IE8, but it may be a known issue or (even more likely) I am approaching things incorrectly and need to be corrected. I would appreciate any assistance you wonderful folks can provide.
When I bind a click handler to an element using jQuery's $.click() as shown below, it generally works fine in IE7/IE8/IE9. Where I run into problems is if the element (in this case a button, but it also applies to other element types from my testing) is inside of a the DOM structure being bound by Knockout. This only occurs in IE7/IE8, sadly, so the jsFiddle link I share at the bottom may be of limited value. In IE9 (unsurprisingly) the behavior works as expected.
This also does not seem to affect events bound without jQuery (using element.attachEvent in pure javascript).
<div data-bind="with:ContainerObject">
<div data-bind="text:ContainedProperty"></div>
<input id="alertButtonJQBad" type="button" value="jq/ko=broken" />
</div>
function jqClickBad() { alert("nested click() handler bound by jquery"); }
// this fires fine in IE9, but not in IE7/IE8:
$("#alertButtonJQBad").click(jqClickBad);
var viewModel = {
ContainerObject: {
ContainedProperty: "test"
}
};
ko.applyBindings(viewModel);
http://jsfiddle.net/csiefken/WckWb/3/
I cannot even run the JS fiddle in IE8. jsFiddle doesn't seem to display properly for me.
Generally speaking, if you are using regular jQuery event bindings instead of KO event bindings then you are going against the grain. If you really need to use jQuery to register your handlers instead of Knockout event bindings, then either do the registration after ko.applyBindings
or use live event handlers (by supplying css selectors to $.on()
) so that your bindings will survive any potential DOM rewrites performed by Knockout.
这篇关于敲除嵌套绑定会干扰IE7 / IE8中的jquery点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!