获取当前的 jQuery 选择器字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9382028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:43:56  来源:igfitidea点击:

Get the current jQuery selector string?

jquerystringjquery-selectors

提问by Richard Testani

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

调用自定义插件时,如何获取当前选择器字符串?

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

Would like to output my_selector pwithin my script. How can I access this string?

my_selector p在我的脚本中输出。我怎样才能访问这个字符串?

回答by Sarfraz

You can use selectorproperty:

您可以使用selector属性:

$('my_selector p').selector // my_selector p

version deprecated:1.7, removed:1.9

不推荐使用的版本1.7删除:1.9

回答by tibc-dev

Post selector deprecation v. 1.7:

后选择器弃用 v. 1.7:

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

如果您仅将 id 和类作为选择器处理,则可以使用以下内容:

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.

有更简洁的方法,但是由于自 1.7 以来浏览器之间的不一致而删除了 .selector,这一直是我的首选。

回答by kevinmicke

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

已弃用,官方建议是在函数调用中添加选择器作为参数: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

需要在其插件中使用选择器字符串的插件可以要求将其作为方法的参数。例如,一个“foo”插件可以写成

$.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.

冗余,是的,​​但它总是可靠的。

回答by EvroMalarkey

Best workaroud is

最好的解决方法是

function mySelector(selector) {
    return $.extend($(selector),{"selector":selector});
}

this returns classic jquery object as $() would

这将像 $() 一样返回经典的 jquery 对象

mySelector(".myClass")

and this returns string of the selector

这将返回选择器的字符串

mySelector(".myClass").selector

回答by bobbdelsol

With sector deprecated i use

随着扇区被弃用,我使用

index = document.getElementById('category').value
select = $("#category option[value=#{index}]")[0].innerHTM

just two lines of code

只需两行代码

or if you're really cheap with lines

或者如果你真的很便宜的话

select = $("#category option[value=#{document.getElementById('category').value}]")[0].innerHTML

回答by wtrevino

jQuery.fn.getSelector = function() {
        return this.data('selector');
};

回答by Imtiaz

Extending to kevinmicke's answer: You can get the selector in your event object that you pass on callback functions.

扩展到 kevinmicke 的答案:您可以在传递回调函数的事件对象中获取选择器。

In callback functions

在回调函数中

event.handleObj.selector

Example:

例子:

You'll get the selector string in e.handleObj.selector

您将在e.handleObj.selector 中获得选择器字符串

    $( '.container' )
    .on('change', 'select.mySelector', function (e) {
        console.log(JSON.stringify(e));
        $('.selector').text(e.handleObj.selector);
        $('.value').text($(this).val());
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

  <select class="mySelector">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </select>
  <h3>Your Selector: <span class="selector"></span></h3>
  <h3>Selected Value: <span class="value"></span></h3>
  
</div>

Console Log gives an object like this:

控制台日志给出了这样的对象:

{
    // ...skipped lines
    "handleObj": {
        "type": "change",
        "origType": "change",
        "guid": 1,
        "selector": "select.mySelector",
        "needsContext": false,
        "namespace": ""
    }
}