[英]jQuery doesn't support .has in IE8? what is a work around?
code: http://jsfiddle.net/4hV6c/4/ just make any selection, and you'll get a script error in ie8 代码: http : //jsfiddle.net/4hV6c/4/进行任何选择,您都会在ie8中收到脚本错误
I'm trying to do this: 我正在尝试这样做:
$(end_node.parentNode).has(start_node)
which in modern browsers (chrome, ff, opera, etc) returns [] if start_node is not in end_node.parentNode, and returns the element (I forget which) if it is found. 如果start_node不在end_node.parentNode中,则在现代浏览器(chrome,ff,opera等)中,哪个返回[],如果找到该元素,则返回元素(我忘记了哪个)。
now, end_node is a text element, and the parentNode is an actual DOM entity. 现在,end_node是一个文本元素,parentNode是一个实际的DOM实体。 IE will perform .has on just $(end_node).has(start_node)
but that is obviously different behavior. IE将仅在$(end_node).has(start_node)
上执行.has,但这显然是不同的行为。
Is there a work around to get this to work? 有没有解决的办法使它起作用?
UPDATE: here is a word around that overrides .has() for my specific scenario.. not sure if it works for all the cases of .has, as I don't know them all. 更新:这是一个单词,在我的特定情况下会覆盖.has()..不确定它是否适用于.has的所有情况,因为我不了解所有情况。 http://jsfiddle.net/8F57r/13/ http://jsfiddle.net/8F57r/13/
The problem is
not
jQuery 问题
不是
jQuery
Running 跑步
console.log( $("div:has(span)").html() );
console.log( $("div").has($("span")[0]).html() );
However, the following throws an exception http://jsfiddle.net/mendesjuan/4hV6c/8/ 但是,以下引发异常http://jsfiddle.net/mendesjuan/4hV6c/8/
var textNode = $("span")[0].childNodes[0];
$("div").has(textNode);
What that means is that you can't pass a text node into $.has
. 这意味着您不能将文本节点传递到$.has
。 You should file a bug with jQuery 您应该使用jQuery提交错误
The line that is erroring out is giving the following message 错误的行给出以下消息
No such interface supported jquery-1.7.1.js, line 5244 character 3
That is trying to call the contains
method on a node. 那是试图在一个节点上调用contains
方法。 What that means is that this really is an IE bug that jQuery hasn't worked around. 这意味着这确实是jQuery尚未解决的IE错误。 I've reproduced the problem without needing to call $.has
http://jsfiddle.net/4hV6c/10/ 我已经重现了这个问题,而无需调用$.has
http://jsfiddle.net/4hV6c/10/
// This is broken in IE
var textNode = $("span")[0].childNodes[0];
var divNode = $("div")[0];
divNode.contains(textNode);
Workaround http://jsfiddle.net/4hV6c/12/ 解决方法 http://jsfiddle.net/4hV6c/12/
function contains(outer, inner) {
var current = inner;
do {
if (current == outer) {
return true;
}
} while((current = current.parentNode) != document.body);
return false;
}
rangy.init();
$(document).bind("mouseup", function() {
var a = rangy.getSelection();
start_node = a.anchorNode;
end_node = a.focusNode;
var b = a.getRangeAt(0);
var c = b.commonAncestorContainer;
b.selectNodeContents(c);
a.setSingleRange(b);
alert( contains( end_node.parentNode, start_node) );
});
You could always DIY? 你总是可以自己动手做吗? :) I hear recursive child indexing works real quick and is pretty easy to implement. :)我听说递归子索引的工作非常迅速,并且很容易实现。
See here for an excellent tutorial: 参见此处以获得出色的教程:
http://blog.swapnilsarwe.com/javascript-traversing-html-dom-recursively.html http://blog.swapnilsarwe.com/javascript-traversing-html-dom-recursively.html
Does find do what you want? 找到你想要的吗?
rangy.init();
$(document).bind("mouseup", function() {
var a = rangy.getSelection();
start_node = a.anchorNode;
end_node = a.focusNode;
var b = a.getRangeAt(0);
var c = b.commonAncestorContainer;
b.selectNodeContents(c);
a.setSingleRange(b);
alert($(end_node.parentNode).find(start_node).length > 0);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.