Get the current jQuery selector string?

前端 未结 8 1977
北恋
北恋 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:27

    Post selector deprecation v. 1.7:

    If you're only dealing with ids and classes as selectors you can use the following:

    var selector = (typeof($(this).attr('id')) !== 'undefined' || $(this).attr('id') !== null) ? '#' + $(this).attr('id') :  '.' + $(this).attr('class');
    

    There are cleaner ways but since the removal of .selector due to being inconsistent between browsers since 1.7, this has been my go-to.

提交回复
热议问题