English 中文(简体)
纤维法的假造者
原标题:Pseudo-selectors with siblings method

早些时候,我回答了,该问题基本上涉及取消表格。 这个问题是对这一问题意见的结果。 鉴于以下超文本:

<div><a href="#" class="removelink">remove</a></div>
<table>
    <tr>
        <td>Row 1</td> 
    </tr>
</table>

The following jQuery:

$( .removelink ).click(function(){
    $(this).parent().siblings( table tr:last ).remove();
});

I would expect nothing to happen, because the siblings method should select the siblings of the currently matched element, optionally filtered by a selector. From the jQuery docs:

The method optionally accepts a selector expression of the same type that we can pass to the $() function. If the selector is supplied, the elements will be filtered by testing whether they match it.

在此基础上,我将上述代码改为“在<条码>>/代码>内作为最后<条码>的现值元(<条码>div>的>。 显然,没有任何内容与这种描述相匹配——在<条码>内有<>t<>/code>,但该编码并不与<条码>d<>d相容。 因此,我不会指望任何内容会归还。 然而,它实际上回到整个表格上来,因为如果它忽视了“<代码>tr:last部分选任人。

我还感到困惑的是,如果你删除<代码>:,pseudo-selector,则该编码按预期运行(没有任何内容)。

Why is the entire table removed by the above code? Am I just being stupid and missing something obvious? You can see the above code in action here.

Edit - Here s a simplified version. Given the following HTML:

<div id="d1"></div>
<div>
    <span></span>
</div>

为什么要归还第二个<条码>div:

$("#d1").siblings("div span:last");

我期望它不会再回头,因为没有“<条码>>span,该编码是<条码>#d1的缩略语。 此处为这一简化的例子提供了一条dle。

<>Update>

在对@muistooshort进行精彩的调查之后,我创建了一个。 追踪这一问题的彩票

最佳回答

请允许我谈谈我的评论。 所有这一切都基于你的第二个简化例子和格鲁斯1.6.4。 这或许是很漫长的,但我们需要通过《 j法》来找出它正在做的事情。


We do have the jQuery source available so let us go a wandering through it and see what wonders there are to behold therein.

<代码>siblings looks:

siblings: function( elem ) {
    return jQuery.sibling( elem.parentNode.firstChild, elem );
}

总结如下:

// `name` is "siblings", `fn` is the function above.
jQuery.fn[ name ] = function( until, selector ) {
    var ret = jQuery.map( this, fn, until )

    //...

    if ( selector && typeof selector === "string" ) {
        ret = jQuery.filter( selector, ret );
    }

    //...
};

然后,j Query.sibling:

sibling: function( n, elem ) {
    var r = [];

    for ( ; n; n = n.nextSibling ) {
        if ( n.nodeType === 1 && n !== elem ) {
            r.push( n );
        }
    }

    return r;
}

So we go up one step in the DOM, go to the parent s first child, and continue sideways to get all of the parent s children (except the node we started at!) as an array of DOM elements.

That leaves us with all of our sibling DOM elements in ret and now to look at the filtering:

ret = jQuery.filter( selector, ret );

因此,<条码>过滤器/代码”究竟是什么? www.un.org/Depts/DGACM/index_spanish.htm

filter: function( expr, elems, not ) {
    //...
    return elems.length === 1 ?
        jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
        jQuery.find.matches(expr, elems);
}

In your case, elems will have have exactly one element (as #d1 has one sibling) so we re off to jQuery.find.matchesSelector which is actually Sizzle.matchesSelector:

var html = document.documentElement,
    matches = html.matchesSelector || html.mozMatchesSelector || html.webkitMatchesSelector || html.msMatchesSelector;
//...
Sizzle.matchesSelector = function( node, expr ) {
    // Make sure that attribute selectors are quoted
    expr = expr.replace(/=s*([^ "]]*)s*]/g, "= $1 ]");

    if ( !Sizzle.isXML( node ) ) {
        try {
            if ( pseudoWorks || !Expr.match.PSEUDO.test( expr ) && !/!=/.test( expr ) ) {
                var ret = matches.call( node, expr );

                // IE 9 s matchesSelector returns false on disconnected nodes
                if ( ret || !disconnectedMatch ||
                        // As well, disconnected nodes are said to be in a document
                        // fragment in IE 9, so check for that
                        node.document && node.document.nodeType !== 11 ) {
                    return ret;
                }
            }
        } catch(e) {}
    }

    return Sizzle(expr, null, null, [node]).length > 0;
};

A bit of experimentation indicates that neither the Gecko nor WebKit versions of matchesSelector can handle div span:first so we end up in the final Sizzle() call; note that both the Gecko and WebKit matchesSelector variants can handle div span and your jsfiddles work as expected in the div span case.

What does Sizzle(expr, null, null, [node]) do? Why it returns an array containing the <span> inside your <div> of course. We ll have this in expr:

 div span:last 

http://www.ohchr.org。

<div id="d2">
    <span id="s1"></span>
</div>

So the <span id="s1"> inside node nicely matches the selector in expr and the Sizzle() call returns an array containing the <span> and since that array has a non-zero length, the matchesSelector call returns true and everything falls apart in a pile of nonsense.

问题是,在这一案件中,与Siezzle的j Query isn t interfacing是适当的。 祝贺者,你是婴儿ug的自豪父亲。

这里有一(男性)的 j,有一对 couple,有一对 couple子(console.log。 呼吁支持我所说的话:

http://jsfiddle.net/ambiguous/TxGXv/

值得注意的是:

  1. You will get sensible results with div span and div span:nth-child(1); both of these use the native Gecko and WebKit selector engine.
  2. You will get the same broken results with div span:first, div span:last, and even div span:eq(0); all three of these go through Sizzle.
  3. The four argument version of the Sizzle() call that is being used not documented (see Public API) so we don t know if jQuery or Sizzle is at fault here.
问题回答

A. 最新情况

$( .removelink ).click(function(){

$(this).parent().siblings( table ).find( tr:last ).remove();

iii

rel=“nofollow”>Fiddle Example





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签