Get the current jQuery selector string?

前端 未结 8 2002
北恋
北恋 2020-12-01 18:00

When calling a custom plugin, how can I get the current selector string?

$(\'my_selector p\').my_plugin();

Would like to output my_se

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 18:22

    Being deprecated, the official advice is to add the selector as a parameter in your function call: https://api.jquery.com/selector/

    Plugins that need to use a selector string within their plugin can require it as a parameter of the method. For example, a "foo" plugin could be written as

    $.fn.foo = function( selector, options ) { /* plugin code goes here */ };
    

    and the person using the plugin would write

    $( "div.bar" ).foo( "div.bar", {dog: "bark"} );
    

    Redundant, yes, but it's always reliable.

提交回复
热议问题