0

jQueryでは、次のように、すべての要素が定義した関数を介して実行されるセレクターを実行できます(完全に考案された例)。

jQuery.expr[':'].AllOrNothing= function(a,i,m){
    // a is the thing to match, m[3] is the input
    if(m[3] === "true"){
      return true;
    } else {
      return false;
    }
  };

次に、次のように使用できます。

$("div:AllOrNothing(" + true + ")"); //returns all divs
$("div:AllOrNothing(" + false + ")"); //returns nothing

呼び出す代わりに無名関数を渡すことは可能jQuery.expr[:].Name=ですか?

編集

私は次のような連鎖可能なものを想像しています:

$("div").filterByFunction(function(a,i,m){ ... })
4

2 に答える 2

1

.filter()組み込みのメソッドを使用して、兄弟要素を調べてtrueまたはfalseのどちらを返すかを決定し、残りの要素を非表示にするカスタム関数を渡したいようです。

$("section").filter(function() {
    // examine child div and return true or false
}).hide();
于 2012-08-13T23:48:16.243 に答える
1

filter完全を期すために、に追加することで自分自身の実装を行うことができます$.fn

$.fn.customFilter = function(f) {
    var filtered = $();
    this.each(function() {
        if(f.call(this)) {
            filtered = filtered.add(this)
        }
    });
    return filtered;
}

$("div").filterByFunction(function(){ return $(this).text() != "test"; })

この場合、そうすべきではありません。

于 2012-08-14T00:34:40.960 に答える