[英]Why does jquery give me a “selector is undefined” error for this code?
我确实在为这个问题而苦苦挣扎,希望这里有人会花一些时间来帮助我。 我正在尝试使用jQuery 1.6.2和JQM 1.0rc3在xml文档中找到名为“ Emotion”的子节点的最后一个文本选择。 我正在使用的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>
以下ajax代码检索了整个xml文档,并且在firebug的控制台中看到了成功的Get。 但是,我还会收到“未定义选择器”错误消息,并且该函数无法像“变量跟踪”声明那样尝试检索Emotion的文本。 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 " + tracking + ':');
//refresh the review data div #summarylist
$('#emotion_type').live("refresh");
});
}
});
我假设错误消息是由“ var tracking”声明引起的。 但是,我的应用程序中的另一个页面几乎运行相同的代码,而没有错误消息。 如果有人愿意让我参与这个问题,我需要两点建议。
我如何声明选择器,您可以看到任何明显的问题吗?
您能否提供一些有关如何修改它的见解,以便它返回xml文档中“ Emotion”的最后一个子节点的文本?
更新:我改为jquery.mobile-1.0.js和jquery-1.6.4.js。 该错误仍然会发生。
您是否以字符串形式获取数据? 在这种情况下,您需要将其放在浏览器窗口中。DOMParser(Firefox,Chrome)或ActiveXObject(IE)。 我怀疑它是真正的选择器,看起来还不错。
另外,最好在迭代之前保存对$(“#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.