如何获取 jQuery 选择器的文本表达式?
-
20-08-2019 - |
题
我有一个 jQuery 选择器,它有一个链式函数。
在函数内部,我想访问代表选择器表达式的 TEXT。
$("cat dog").function() {
// how do I get access to the "cat dog" string from inside THIS function ?
};
我在这个代码示例中过度简化了我真正想要做的事情。我正在编写一个插件,我需要访问已为其创建包装集的选择器。显然,在这个特定的例子中,我可以访问“猫狗”,因为我写了它。所以想象一下这是在一个插件中。
谷歌搜索这个有点棘手。
编辑: 不幸的是,“selector”属性现在已被弃用。 http://jquery.com/upgrade-guide/1.9/#selector-property-on-jquery-objects
解决方案
有在jQuery对象一个“选择”属性,但我不知道它总是可用的。
其他提示
这是远远没有达到最佳,但在某些情况下工作。你可以做到以下几点:
jQuery.fn._init = jQuery.fn.init
jQuery.fn.init = function( selector, context ) {
return (typeof selector === 'string') ? jQuery.fn._init(selector, context).data('selector', selector) : jQuery.fn._init( selector, context );
};
jQuery.fn.getSelector = function() {
return jQuery(this).data('selector');
};
这将返回用于该元素的最后一个选择器。但它不会对非现有元素工作。
<div id='foo'>Select me!</div>
<script type='text/javascript'>
$('#foo').getSelector(); //'#foo'
$('div[id="foo"]').getSelector(); //'div[id="foo"]'
$('#iDoNotExist').getSelector(); // undefined
</script>
此工作与jQuery 1.2.6和1.3.1和可能的其它的版本。
此外:
<div id='foo'>Select me!</div>
<script type='text/javascript'>
$foo = $('div#foo');
$('#foo').getSelector(); //'#foo'
$foo.getSelector(); //'#foo' instead of 'div#foo'
</script>
修改的结果, 如果您immidiatly检查选择已被使用后,你可以用你的插件如下:
jQuery.getLastSelector = function() {
return jQuery.getLastSelector.lastSelector;
};
jQuery.fn._init = jQuery.fn.init
jQuery.fn.init = function( selector, context ) {
if(typeof selector === 'string') {
jQuery.getLastSelector.lastSelector = selector;
}
return jQuery.fn._init( selector, context );
};
那么下面将工作:
<div id='foo'>Select me!</div>
<script type='text/javascript'>
$('div#foo');
$.getLastSelector(); //'#foo'
$('#iDoNotExist');
$.getLastSelector(); // #iDoNotExist'
</script>
在你的插件,你可以这样做:
jQuery.fn.myPlugin = function(){
selector = $.getLastSelector;
alert(selector);
this.each( function() {
//do plugins stuff
}
}
$('div').myPlugin(); //alerts 'div'
$('#iDoNotExist').myPlugin(); //alerts '#iDoNotExist'
但是仍然:
$div = $('div');
$('foo');
$div.myPlugin(); //alerts 'foo'
如果您正在使用萤火虫,你可以console.log(this)
在函数内部,看看是否选择字符串访问某处对象。对不起,我不熟悉的jQuery的API。
如果您要访问您的功能选择字符串这将工作:
$(this).html();
这也将工作,如果多个选择器是使用,
例如
$('#id1,#id2').click(function(){
alert($(this).html());
});
这也许解决您的问题:
var $jqueryItems = $( ".my-selector" );
console.log( $jqueryItems.selector ); // display ".my-selector"
对于那些想要进入给 jQuery 的插件选择器字符串的人,我很高兴改进了 @Pim Jager 给出的很好的答案:
- 截至目前(最新版本 3.2.1),有 3 个参数
jQuery.fn.init
功能 -selector, context, root
- 不是两个; new
关键字应该添加到 return 语句中jQuery.fn.init
;- 在你的插件内部,选择器通过调用以字符串形式返回
$(this).getSelector();
最后,这就是我必须为我工作的魅力:
(function($, window, document, undefined) {
$.fn._init = $.fn.init
$.fn.init = function( selector, context, root ) {
return (typeof selector === 'string') ? new $.fn._init(selector, context, root).data('selector', selector) : new $.fn._init( selector, context, root );
};
$.fn.getSelector = function() {
return $(this).data('selector');
};
$.fn.YOUR-PLUGIN = function() {
var selector = $(this).getSelector(); // selectors string given to jQuery
// other code
}
})(jQuery, window, document);
就我而言,它适用于 jQuery 版本(从 1.7.0 到 3.2.1)。
聚苯乙烯. 。即使使用 stackoverflow 上提供的 jQuery 1.2.3 也可以正常工作。所以,我想,如果您想将 jQuery 选择器的表达式作为插件内的文本获取,则以下内容适用于所有 jQuery 版本:
// our plugin
(function($, window, document, undefined) {
$.fn._init = $.fn.init
$.fn.init = function( selector, context, root ) {
return (typeof selector === 'string') ? new $.fn._init(selector, context, root).data('selector', selector) : new $.fn._init( selector, context, root );
};
$.fn.getSelector = function() {
return $(this).data('selector');
};
$.fn.coolPlugin = function() {
var selector = $(this).getSelector();
if(selector) console.log(selector); // outputs p #boldText
}
})(jQuery, window, document);
// calling plugin
$(document).ready(function() {
$("p #boldText").coolPlugin();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<p>some <b id="boldText">bold text</b></p>
我无法找出如何获得参考提供给内部$选择的字符串值(“value_i_want_here”)当结合于一个功能;似乎是一个棘手的问题。但是,如果你喜欢在IE测试[X]总是有 Firebug的精简版这作品真的很好利用的console.log(VAR | VAL)
好了,我还在试图找出如果你想获取该元素的名称?或者ID和/或类别或全部。
您可以得到这个元素
var element =$(this).get(0); var id = $(this).attr('id'); var class=$(this).attr('class');
不知道你是否有一个以上的类时会发生什么。可能会进入一个数组或什么的,你可以得到与索引。