-1

たとえば、リンクのグループはほとんどありません。

<a href="http://google.com/b">http://google.com/b</a>
<a href="http://google.com/a">http://google.com/a</a>
<a href="http://stackoverflow.com/g">http://stackoverflow.com/g</a>
<a href="http://google.com/c">http://google.com/c</a>
<a href="http://stackoverflow.com/a">http://stackoverflow.com/a</a>
<a href="http://en.wikipedia.org/c">http://en.wikipedia.org/c</a>
<a href="http://en.wikipedia.org/a">http://en.wikipedia.org/a</a>
<a href="http://en.wikipedia.org/b">http://en.wikipedia.org/b</a>

google.com のリンクと wikipedia.org のリンクを並べ替えて、その位置を維持する必要がありますが、私の質問は並べ替えに関するものではありません。要素のグループでの反復に関する私の質問。私の例では、次のようになります。

$("a[href*='google.com'], a[href*='en.wikipedia.org']").each(function(){

  // I need in $(this) google.com links in first iteration, and wikipedia.org links in second 

});

それを行うためのネイティブな方法はありますか?

PS wrap() は不可能であり、追加の機能を実行したくありません。

4

3 に答える 3

2

代わりに以下を使用できます。

$('a[href*="google.com"]').add('a[href*="en.wikipedia.org"]').each(...);

例については、このjsFiddleを参照してください。

于 2013-07-30T22:57:19.727 に答える
2

ドメインごとにグループで処理しようとしているだけの場合は、ドメインを繰り返し処理して、関連する要素を見つけることができます。

$.each(['google.com', 'en.wikipedia.org'], function (_, domain) {
    var links = $('a[href*="' + domain + '"]');

    // 1st round will have `google.com` links, 2nd `en.wikipedia.org`
});

.wrapAll()次に、各コレクションを単一の要素にラップするために使用できます。

links.wrapAll('<div>');

例: http://jsfiddle.net/AmYCk/

<div>
    <a href="http://google.com/b">http://google.com/b</a>
    <a href="http://google.com/a">http://google.com/a</a>
    <a href="http://google.com/c">http://google.com/c</a>
</div>

<a href="http://stackoverflow.com/g">http://stackoverflow.com/g</a>
<a href="http://stackoverflow.com/a">http://stackoverflow.com/a</a>

<div>
    <a href="http://en.wikipedia.org/c">http://en.wikipedia.org/c</a>
    <a href="http://en.wikipedia.org/a">http://en.wikipedia.org/a</a>
    <a href="http://en.wikipedia.org/b">http://en.wikipedia.org/b</a>
</div>
于 2013-07-30T23:00:36.007 に答える
0

改良版

$([$("a"), $("span")]).each(function(){
  console.log($(this).length);
});

jsbin.com

于 2013-09-09T06:01:07.690 に答える