简体   繁体   English

为什么jquery会给我这个代码一个“选择器未定义”的错误?

[英]Why does jquery give me a “selector is undefined” error for this code?

I am really struggling with an issue that I hope someone here will take some time to help me with. 我确实在为这个问题而苦苦挣扎,希望这里有人会花一些时间来帮助我。 I'm trying to find the last text selection for a child node named "Emotion" in an xml document using jQuery 1.6.2 and JQM 1.0rc3. 我正在尝试使用jQuery 1.6.2和JQM 1.0rc3在xml文档中找到名为“ Emotion”的子节点的最后一个文本选择。 The XML document I'm working with has the following structure. 我正在使用的XML文档具有以下结构。

<StatusModel>
 <Uid>5e4dc88f-f703-4e34-92f6-609216de6c6f</Uid>
 <Created>Wednesday, November 02, 2011 2:20 PM</Created>
 <User>larry_irons</User>
 <Level>2</Level>
 <Doing>typing</Doing>
 <Thinking>the pain in my neck</Thinking>
 <Emotion>Pain</Emotion>
</StatusModel>

The following ajax code retrieves the entire xml document and I see the succesful Get in firebug's console. 以下ajax代码检索了整个xml文档,并且在firebug的控制台中看到了成功的Get。 However, I also receive a "selector is undefined" error message and the function fails to retrieve the text for Emotion as the "var tracking" declaration attempts to do. 但是,我还会收到“未定义选择器”错误消息,并且该函数无法像“变量跟踪”声明那样尝试检索Emotion的文本。 The ajax is as follows: ajax如下:

$('#feeling').live('pageinit', function (event) {


    $.ajax({
        type: "Get",
        url: "/api/list",
        cache: false,
        dataType: 'xml',
        success: function (xml) {

            manipulateType(xml);
        }


    });

    function manipulateType(xml) {

        //empty the emotion_type div
        $("#emotion_type").html("");

        //retrieve current emotion to track and append to div emotion_type
        $(xml).find('StatusModel').each(function () {

            var tracking = $(this).find("Emotion").text();


            //append content and tracking variable to div emotion_type
            $('#emotion_type').append("On a scale of 1 (low) to 10 (high) I'm feeling this much&nbsp;" + tracking + ':');

            //refresh the review data div #summarylist
            $('#emotion_type').live("refresh");

        });
    }
});

I'm assuming the error message is resulting from the "var tracking" declaration. 我假设错误消息是由“ var tracking”声明引起的。 However, another page in my application runs virtually the same code without an error message. 但是,我的应用程序中的另一个页面几乎运行相同的代码,而没有错误消息。 I need two points of advice if someone is willing to engage me on this problem. 如果有人愿意让我参与这个问题,我需要两点建议。

Can you see any obvious problem with how I'm declaring the selector? 我如何声明选择器,您可以看到任何明显的问题吗?

Can you offer some insight into how I can modify it so that it returns the text of the last child node for "Emotion" in the xml document? 您能否提供一些有关如何修改它的见解,以便它返回xml文档中“ Emotion”的最后一个子节点的文本?

Update: I changed to jquery.mobile-1.0.js and jquery-1.6.4.js. 更新:我改为jquery.mobile-1.0.js和jquery-1.6.4.js。 The error still occurs. 该错误仍然会发生。

Are you getting the data as a string? 您是否以字符串形式获取数据? In that case you need to put it through the browsers window.DOMParser (firefox, chrome) or ActiveXObject (IE). 在这种情况下,您需要将其放在浏览器窗口中。DOMParser(Firefox,Chrome)或ActiveXObject(IE)。 I doubt it is the actual selector, that looks fine. 我怀疑它是真正的选择器,看起来还不错。

Also, it is good to save your reference to $("#emotion_type") before you iterate (and choose a better name :). 另外,最好在迭代之前保存对$(“#emotion_type”)的引用(并选择一个更好的名称:)。

function manipulateType(xml) {
    var emo = $("#emotion_type");

    //empty the emotion_type div
    $(emo).html("");

    //retrieve current emotion to track and append to div emotion_type
    $(xml).find('StatusModel').each(function () {

        var tracking = $(this).find("Emotion").text();


        //append content and tracking variable to div emotion_type
        $(emo).append("On a scale of 1 (low)...");

        //refresh the review data div #summarylist
        $(emo).live("refresh");

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM