jQuery 选择器返回 prevObject 而不是普通元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13306832/
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-26 12:29:44  来源:igfitidea点击:

jQuery selector returns prevObject instead of normal element

jqueryjquery-pluginsjquery-selectors

提问by Jacob

I have some problems with selecting elements, with Jquery. When i try to select a element:

我在使用 Jquery 选择元素时遇到了一些问题。当我尝试选择一个元素时:

var images = $("#htmlChunk").find("img.Thumb");
console.log(images);

i get this result:

我得到这个结果:

>[<img>, <img>, prevObject: e.fn.e.init[1], context: #document, selector: "#htmlChunk img.Thumb"]

What is causing this returned result? I tried some things but still dont get the result i wanted.

是什么导致了这个返回的结果?我尝试了一些东西,但仍然没有得到我想要的结果。

i tried to wrap the code to avoid conflict's. i tried to clear the object

我试图包装代码以避免冲突。我试图清除对象

This was something i found on the web. http://drupal.org/node/272557

这是我在网上找到的东西。 http://drupal.org/node/272557

var images = $("#htmlChunk")['prevObject'].find("img.Thumb");

i get returned a object now, but thats also not what i wanted.

我现在得到了一个对象,但这也不是我想要的。

I jumped into this project so i am not well-known with the script. i tried to search for prevObject in the js files but could'nt find any.

我加入了这个项目,所以我对脚本并不熟悉。我试图在 js 文件中搜索 prevObject 但找不到任何。

I think the problem is that it is interfering with some other javascript file. any ideas? directions?

我认为问题在于它干扰了其他一些 javascript 文件。有任何想法吗?方向?

Edit: htmlChunk:

编辑:htmlChunk:

<div id="htmlChunk">
    <div class="ngg-albumoverview">
        <div class="ngg-album-compact">
            <div class="ngg-album-compactbox">
                <div class="ngg-album-link">
                    <a class="Link" href="http://........">
                        <img class="Thumb" alt="Personeelsevent" src="http://.........">
                    </a>
                </div>
            </div>
            <h4><a class="ngg-album-desc" title="Personeelsevent" href="http://.....">Personeelsevent</a></h4>
            <p><a href="http:///.......">bekijk dit album</a></p>
        </div>
    </div>
</div>

回答by Anthony Grist

Your imagesvariable is a jQuery object, so what you're seeing output in your browser's console seems to be that object. The specific output suggests that the call to .find()isn't actually matching any elements; compare the two console outputs from this jsFiddle(in Chrome).

您的images变量是一个 jQuery 对象,因此您在浏览器控制台中看到的输出似乎就是该对象。特定输出表明调用.find()实际上不匹配任何元素;比较来自这个 jsFiddle(在 Chrome 中)的两个控制台输出。

When you call a jQuery function - such as .find(), .filter(), etc - that narrows down, or changes, the list of matched elements on an existing jQuery object the resulting jQuery object also contains a reference to the state before that function was run, which is what you're seeing as prevObject. This is what it uses to revert back to when you call the .end()function.

当你调用一个jQuery功能-比如.find().filter()等等-对缩小了,或改变现有的jQuery对象匹配的元素列表中所产生的jQuery对象还包含了对国家的参考之前函数运行,这是什么你看到的是prevObject. 这是它在调用.end()函数时用于恢复的内容。

Let's break down your code:

让我们分解你的代码:

var images = $(".htmlChunk").find("img.Thumb");

The first part - $(".htmlChunk")- matches all elements that have the class htmlChunkon them, and returns a jQuery object containing those elements.

第一部分 - $(".htmlChunk")- 匹配所有具有该类的元素htmlChunk,并返回一个包含这些元素的 jQuery 对象。

Then you call .find("img.Thumb")which looks for all elements that are descendents of the already matched elements (those with the class htmlChunk) that satisfy the criteria of being an <img>element and having the class Thumbon them.

然后调用.find("img.Thumb")which 查找所有htmlChunk满足作为<img>元素并在其Thumb上具有类的条件的已匹配元素(具有 class 的元素)的后代的元素。

You could use a single selector to retrieve the elements, which might give you better results:

您可以使用单个选择器来检索元素,这可能会给您带来更好的结果:

var images = $(".htmlChunk img.Thumb");

If you want an array of the actual DOM elements, rather than a jQuery object containing them, you can use the .get()function:

如果你想要一个实际 DOM 元素的数组,而不是一个包含它们的 jQuery 对象,你可以使用这个.get()函数:

var elementArray = images.get();


To address the edit to the question to include the HTML:

要解决对问题的编辑以包含 HTML:

You're using $(".htmlChunk")to obtain the initial element. However, that <div>element has an ID, not a class, of htmlChunkso that code won't select the element you need. You'll want to use the following:

您正在使用$(".htmlChunk")获取初始元素。但是,该<div>元素具有ID而不是类 of,htmlChunk因此代码不会选择您需要的元素。您将需要使用以下内容:

var images = $("#htmlChunk").find("img.Thumb");

Note the #rather than the .in the selector.

注意选择器中的#而不是.

回答by anderssonola

It looks like your trying to select class ".htmlChunk", but does not exist. Try selecting by id instead.

看起来您尝试选择类“.htmlChunk”,但不存在。尝试按 id 选择。

var images = $("#htmlChunk").find("img.Thumb");

回答by xdeepakv

try this.

尝试这个。

   function getId() {
             var ids = $(".htmlChunk").find("img.thumb"); //it wiill return array of image
             var sid = $(".htmlChunk").find("img#img2"); //it wiill return wat image you want
             $(".htmlChunk span").text("arrays:" + ids[0] + "  :  " + ids[1] +
             "simple imge:" + sid);
         }


    <input id="Button1" type="button" value="button" onclick= "getId();"/>


  <div class="htmlChunk">
        <img alt="" src="img/Desert.jpg" class="thumb" id="img1" />
       <img alt="" src="img/Chrysanthemum.jpg" class="thumb" id="img2" />
        <span></span>
   </div>

回答by devman

isn't prevObject just a property of the result?

是不是 prevObject 只是结果的一个属性?

you could understand it as "the result has the following prevObject ->"

您可以将其理解为“结果具有以下 prevObject ->”