Javascript E感谢关于$(“div:has(“#myelementid”)”示例,目标元素(即#myelementid)需要是应用查找选择器的元素,因为它可能是动态的(例如,单击的元素等)而且可能没有身份证。然而,你给了我一些新的想法来继续。@adeneo提供了一个有

Javascript E感谢关于$(“div:has(“#myelementid”)”示例,目标元素(即#myelementid)需要是应用查找选择器的元素,因为它可能是动态的(例如,单击的元素等)而且可能没有身份证。然而,你给了我一些新的想法来继续。@adeneo提供了一个有,javascript,jquery,jquery-selectors,sizzle,Javascript,Jquery,Jquery Selectors,Sizzle,E感谢关于$(“div:has(“#myelementid”)”示例,目标元素(即#myelementid)需要是应用查找选择器的元素,因为它可能是动态的(例如,单击的元素等)而且可能没有身份证。然而,你给了我一些新的想法来继续。@adeneo提供了一个有趣的黑客,证明它确实是可能的:现在只需要找到一个更整洁的方法:)这很容易修复,只是为了证明它是可以做到的。然而,它应该通过连接到jQuery.fn.init来完成,不知何故,检查选择器并在上拆分。这个答案根本没有回答任何问题,它只是突出了关于伪


E感谢关于
$(“div:has(“#myelementid”)”
示例,目标元素(即
#myelementid
)需要是应用查找选择器的元素,因为它可能是动态的(例如,单击的元素等)而且可能没有身份证。然而,你给了我一些新的想法来继续。@adeneo提供了一个有趣的黑客,证明它确实是可能的:现在只需要找到一个更整洁的方法:)这很容易修复,只是为了证明它是可以做到的。然而,它应该通过连接到
jQuery.fn.init
来完成,不知何故,检查选择器并在
上拆分。这个答案根本没有回答任何问题,它只是突出了关于伪选择器如何工作的问题下的注释中已经存在的内容,顺便说一句,定义自定义表达式的方式在jQuery 1.8中发生了更改,此处发布的方式是定义伪选择器的旧方式?我不建议这样做,因为这样会弄乱jQuery内部,甚至会更改元素ID!,您可以打破依赖于jQuery的东西(例如bootstrap),除非您准备好处理生产中的意外错误。。。我将发布另一个答案,以避免这种黑客行为,并且更接近您所寻找的替换
find
可能是合理的,尽管引入一种新方法会更好。但是,使用任意id
findme123
是非常值得怀疑的,应该用适当的
.parent(…).find(…)
调用来代替。@dseminara:如果检查代码,您将看到id立即被还原。您还将注意到,除非选择器包含:this,否则它只调用通常的find代码。如果您能找到实际问题或更好的解决方案,请列出:)@Bergi:最好添加新方法。“findThis”怎么样?开销是对所有其他发现的索引调用,这很小,但我接受你的观点。id应该是唯一的(因此旁边有注释),但我没有时间添加生成的id。将更正这些问题并发布更新。谢谢。在异常情况下不安全,如果在还原id之前发生异常,它将保持错误:(
jQuery.extend(jQuery.expr[':'], {
    focusable: function (el, index, selector) {
        return $(el).is('a, button, :input[type!=hidden], [tabindex]');
    };
});
$(':focusable').css('color', 'red');  // color all focusable elements red
$('input:focus').closest('.form-group').find('.label');
$('input:focus < .form-group .label');
$('input:focus:closest(.form-group) .label');
options.selector = ':closest(".form-group") .label';

$('input').click(function(){
    var label = $(this).find(options.selector);
});
 jQuery.expr.match.closest = /^:(?:closest)$/;
 jQuery.expr.find.closest = function (match, context, isXML){
     console.log("jQuery.expr.find.closest");
 };
// Fetch a seed set for right-to-left matching
i = matchExpr["needsContext"].test(selector) ? 0 : tokens.length;
while (i--) {
    token = tokens[i];

    // Abort if we hit a combinator
    if (Expr.relative[(type = token.type)]) {
        break;
    }
    if ((find = Expr.find[type])) {
        // Search, expanding context for leading sibling combinators
        if ((seed = find(
            token.matches[0].replace(runescape, funescape),
            rsibling.test(tokens[0].type) && testContext(context.parentNode) || context
        ))) {

            // If seed is empty or no tokens remain, we can return early
            tokens.splice(i, 1);
            selector = seed.length && toSelector(tokens);
            if (!selector) {
                push.apply(results, seed);
                return results;
            }

            break;
        }
    }
}
jQuery.extend(jQuery.expr[':'], {
  focusable: function (el, index, selector) {
    return $(el).is('a, button, :input[type!=hidden], [tabindex]');
  };
})
$("div:empty")
$("div").filter(":empty")
function ( text ) {
        return function( elem ) {
            return ( elem.textContent || elem.innerText || getText( elem ) ).indexOf( text ) > -1;
        };
}
<element1>
  <div>
    <span id="myelementid"></span>
  </div>
  <div>
    <p>Lorem ipsum sir amet dolor... </p>
  </div>
</element1>
<element2>
</element2>
$("*:has('#myelementid')")
$("div:has('#myelementid')")
$(".agivenclass:has('#myelementid')")
$('#myelementid').parents()
$('#myelementid').parents(null, "div")
$('#myelementid').parents().filter("div")
// Find all labels under .level3 classes that have the .starthere class beneath them
$('.starthere').findThis('.level3:has(:this) .label')
$('.starthere').parents('.level3').find('.label')
// Add findThis method to jQuery (with a custom :this check)
jQuery.fn.findThis = function (selector) {
    // If we have a :this selector
    if (selector.indexOf(':this') > 0) {
        var ret = $();
        for (var i = 0; i < this.length; i++) {
            var el = this[i];
            var id = el.id;
            // If not id already, put in a temp (unique) id
            el.id = 'id'+ new Date().getTime();
            var selector2 = selector.replace(':this', '#' + el.id);
            ret = ret.add(jQuery(selector2, document));
            // restore any original id
            el.id = id;
        }
        ret.selector = selector;
        return ret;
    }
    // do a normal find instead
    return this.find(selector);
}

// Test case
$(function () {
    $('.starthere').findThis('.level3:has(:this) .label').css({
        color: 'red'
    });
});
// Save the original jQuery find we are replacing
jQuery.fn.findorig = jQuery.fn.find

// Replace jQuery find with a custom :this hook
jQuery.fn.find = function (selector) {
    // If we have a :this selector
    if (selector.indexOf(':this') > 0) {
        var self = this;
        var ret = $();
        for (var i = 0; i < this.length; i++) {
            // Save any existing id on the targetted element
            var id = self[i].id;
            if (!id) {
                // If not id already, put in a temp (unique) one
                self[i].id = 'findme123';
            }
            var selector2 = selector.replace(':this', '#findme123');
            ret = ret.add(jQuery(selector2, document));
            // restore any original id
            self[i].id = id;
        }
        ret.selector = selector;
        return ret;
    }
    return this.findorig(selector);
}

// Test case
$(function () {
   $('.starthere').find('.level3:has(:this)').css({
        color: 'red'
    });
});
options.selector = ".form-group:has(:this) .label";

$('input').click(function(){
    var label = $(this).find(options.selector);
});