4

現在の要素の配列を作成したメソッドの名前を取得できるかどうか疑問に思っていました。

jqueryオブジェクト自体で見つけようとしましたが、保存できる場所がわかりません。

これを埋めてみてください

$.fn.myfunc=function(){
//your brilliant idea here
return functname;
}

$('body').find('.a').myfunc(); //returns 'find'
$('body').children('.a').myfunc(); //returns 'children'
$('body').find('.a').next('div').myfunc(); //returns 'next'

//and if You're really awesome:
    $('body').find('.a').next('div').css('float','left').myfunc(); //returns 'next'
4

2 に答える 2

1

この例は完全ではありませんが、多くの状況 (検索、フィルター、子、次) の最後の操作を抽出します - http://jsfiddle.net/X7LmW/3/。jQuery.pushStack http://github.com/jquery/jquery/blob/master/src/core.js#L204の内部に基づく

function last_operation( $$ ) {
    var selector = $$.selector,
        selector_cmpr;

    while ( ( selector_cmpr = remove_paren( selector ) ) != selector ) {
        selector = selector_cmpr;
    }

    var operations = selector.split('.'),
        is_find    = selector.replace(/, /, '').split(' ').length > 1,
        operation;

    if ( is_find ) {
        operation = 'find';
    } else if ( operations.length > 1 ) {
        operation = operations[ operations.length - 1 ].replace(/PAREN/, '')
    } else {
        operation = 'unknown';
    }
    return operation;

    function remove_paren( str ) {
        var str_cmpr = str.replace(/\([^()]+\)/, 'PAREN');
        return str_cmpr;
    }
}
于 2010-09-21T23:31:37.447 に答える
0

バウンティは道を見つけるために BBonified に行きます。

これは、last_operation 関数のアップグレードです。$() は意図的に .find() として認識されます。

$.fn.lastop=function(){
var s=this.selector.replace(/^.*\.([a-zA-Z]+)\([^()]*\)[^ ()]*$|.*/,'$1');
return s?s:'find';
}

これはここで使用されました: http://www.jsfiddle.net/naugtur/rdEAu/

于 2010-09-28T09:13:47.137 に答える