Does jquery not support .has in IE8? What is work?

code: http://jsfiddle.net/4hV6c/4/ just make any choice and you will get a script error in ie8

I am 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 an element (I forget that) if it is found.

now, end_node is a text element, and parentNode is the actual DOM object. IE will execute .has only $(end_node).has(start_node), but this is obviously a different behavior.

Is there any work to make this work?

  • There will be an error in IE script, other browsers will warn you with a boolean value.

UPDATE: here is a word around that overrides .has () for my specific scenario .. not sure if it works for all .has cases, since I don't know all of them. http://jsfiddle.net/8F57r/13/

+5
source share
3 answers

The problem is not jQuery

Launch

console.log( $("div:has(span)").html() );
console.log( $("div").has($("span")[0]).html() );

However, the following excludes http://jsfiddle.net/mendesjuan/4hV6c/8/

var textNode =  $("span")[0].childNodes[0];
$("div").has(textNode);

This means that you cannot pass node text to $.has. You must specify an error using jQuery

The line that produces the error contains the following message

No such interface supported jquery-1.7.1.js, line 5244 character 3

contains node. , IE, jQuery . , $.has http://jsfiddle.net/4hV6c/10/

// This is broken in IE
var textNode =  $("span")[0].childNodes[0];
var divNode = $("div")[0];
divNode.contains(textNode);

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) );
});
+4

Does what you want find?

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);
});​
0
source

All Articles