JQuery / Sizzle selector to find the current parent of an element?

Is there a way to get the selected element parent using only jQuery / sizzle CSS selectors?

I need to be able to get the parent when using jQuery, but I am unable to use jQuery('#myelement').parent(), since I get the selector as a string, and the "user" should be able to go back to the tree.

I do not see anything in the documents, so I wonder if it is documented or if there is a hack that I can use?

+3
source share
4 answers

CSS , XPath, < , .parent() . , .

0

( , jQuery, jQuery API):

$(':has(> #myElement)');

, " " "has", . , , , <.

API jQuery , :

$(document).on('click', ':has(> .widget)', function () {
    // ...
});

.

+3

, CSS jQuery/Sizzle.

?

, -:

jQuery.expr[":"].getParent = function (node, index, prop, nodes) {
    return jQuery(node).parent(); 
};

( , , getParent):

jQuery('#myelement:getParent');
+1

jQuery(jQuery('#myelement').parent()) .

, ;)

, , , .

jQuery('#'+jQuery('#myelement').parent().attr('id')).live(something);

, , . ,

    //newline for reading comfort. skip it. --v
jQuery('[title='+jQuery('#myelement').parent() 
.attr('title',Math.round(Math.random()*100000)+']').attr('title')).live(something);

, ,

, css - , css

0

Source: https://habr.com/ru/post/1735566/


All Articles