为什么jQuery中的.selector属性没有存储有效的选择器值? [英] Why does the .selector property in jQuery not store a valid selector value?

查看:153
本文介绍了为什么jQuery中的.selector属性没有存储有效的选择器值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有与此类似的HTML:

Assuming I have HTML similar to this:

<div class="fooContainer">
    <div class="barContainer">
        <a href="#">foo-bar</a>
    </div>
</div>

如果在脚本中我的入口点是锚点,并且我需要使用fooContainer类到达div,则可以执行以下操作:

If in script my entry point is the anchor and I need to get to the div with the class fooContainer I can do this:

var $fooContainer = $("a").parents(".fooContainer");

这非常完美,因为$fooContainer现在在DOM中保留了对该元素的引用.

This works perfectly fine as $fooContainer now holds the reference to the element in the DOM.

当我现在打印出selector值时,如下所示:

When I now print out the selector value like this:

console.log($fooContainer.selector)

我得到的值是a.parents(.fooContainer).

给我的印象是selector属性返回的字符串本身就是一个有效的选择器值.

I was under the impression that the selector property returned a string which itself would be a valid selector value.

情况并非如此,就像尝试将其用作选择器时一样:

This is not the case though as when trying to use it as a selector like this:

$("a.parents(.fooContainer)")

在jQuery 1.7.2中找不到匹配项.
并且在最新的jQuery 1.8.x中,它甚至引发异常:Error: Syntax error, unrecognized expression: a.parents(.fooContainer)

It cannot find a match in jQuery 1.7.2.
And in the latest jQuery 1.8.x it even throws an exception: Error: Syntax error, unrecognized expression: a.parents(.fooContainer)

  • 为什么选择器属性不包含有效的选择器值?
  • 如果不是,那么将selector属性用于什么呢?
  • Why does the selector property not contain a valid selector value?
  • If it doesn't what would one use the selector property for?

我尝试在jQuery 文档 中搜索信息,但找不到任何内容与该财产有关.

I tried searching the jQuery documentation for information but was not able to find anything related to that property.

推荐答案

您可以轻松阅读源代码代码,但是未浏览的API中未记录的 是内部的和私有的.

You can easily read the source code but what isn't documented in the browsable API is internal and private.

按定义.

所以这是一个实现细节,您实际上不应该尝试使用该属性,因为不能保证将来的版本以及您今天可能想到的任何用途.

So this is an implementation detail, and a property you really shouldn't try to use as there is no guarantee, neither for the future versions nor for any use you might imagine today.

这篇关于为什么jQuery中的.selector属性没有存储有效的选择器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆