【问题标题】:Retrieve the string representation of a jQuery selector检索 jQuery 选择器的字符串表示
【发布时间】:2011-09-08 15:28:30
【问题描述】:
【问题讨论】:
标签:
jquery
string
selector
【解决方案1】:
正如您链接到的问题的答案中所提供的,selector 属性是您能找到的最合适的方法。
虽然对于基本选择器来说它是完全准确的,但您会发现,当使用 parent()、next() 等操作方法时,.selector 属性将变得无法重用:
alert($('foo[id^=fish]:gt(5):eq(4)').selector); // foo[id^=fish]:gt(5):eq(4)
alert($('foo').children('bar').next().selector); // foo.children(bar).next()
例如您可以将第一个直接插入$(),但不能插入后者。
【解决方案2】:
$('#my-id .my-class a').selector;
【解决方案3】:
jqElement.selector
将返回选择器值
【解决方案4】:
selector 属性公开了一个 jQuery 对象的选择器。比如……
var cache = $("#my-id .my-class a");
alert(cache.selector); // will alert "#my-id .my-class a"