[英]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.