[英]Why do does my jquery variable return undefined in firebug console
Here is my code: 这是我的代码:
var altArr = [];
i = 0;
$('ul.ctopics.topics img').each(function () {
altArr[i++] = $(this).attr('alt')
});
var linkArr = [];
c = 0;
$('ul.ctopics.topics .activityinstance a').each(function () {
linkArr[c++] = $(this).attr('href')
});
a = 0;
var scormId = [];
$('ul.ctopics.topics li.modtype_scorm').each(function () {
scormId[a++] = $(this).attr('id')
});
b = 0;
var idArr = [];
$('.course-matrix .course-matrix-table tr').addClass('locked');
$('.course-matrix .course-matrix-table tr').each(function () {
idArr[b++] = $(this).attr('id');
idTr = $(this).attr('id');
var matchArr = $.inArray(idTr, scormId);
var selLink = linkArr[matchArr];
var selAlt = altArr[matchArr];
$(this).find('td.course-title').wrapInner('<a href="' + selLink + '" class="scorm-link"></a>');
$(this).find('a.scorm-link').attr('title', selAlt);
var alt = $(this).find('a.scorm-link').attr('title');
var indexResult = alt.search("Not");
if (indexResult >= 0) {
$(this).removeClass('locked');
$(this).addClass('not-yet-complete');
$(this).addClass('unlocked');
} else {
$(this).addClass('completed');
$(this).addClass('unlocked');
$(this).removeClass('locked');
}
});
I can't find why I'm getting this error from Firebug. 我找不到从Firebug收到此错误的原因。 This breaks my succeeding JS codes:
这破坏了我以后的JS代码:
TypeError: alt is undefined
var indexResult = alt.search("Not");
spoiler to get rid of "It looks like your post is mostly code; please add some more details."
破坏者摆脱“看起来您的帖子主要是代码;请添加更多详细信息。”
You are adding DOM elements dynamically and then searching for them. 您正在动态添加DOM元素,然后搜索它们。 But as you add them, you can assign to a variable.
但是在添加它们时,您可以分配给变量。 Such as this (untested) but you get the idea:
像这样(未经测试),但您会明白:
Replace this: 替换为:
$(this).find('td.course-title').wrapInner('<a href="'+selLink+'" class="scorm-link" title=""></a>');
$(this).find('a.scorm-link').attr('title', selAlt);
var alt = $(this).find('a.scorm-link').attr('title');
With something like this: 用这样的东西:
//first create your nodes, and remember them as variables. Use the $ prefix so you know it is a jquery object that is stored in the variable
var $td = $(this).find('td.course-title');
var $a = $('<a href="'+selLink+'" class="scorm-link"></a>');
//add the link to the cell
$td.append($a);
//then set your attribute. Note you already have the variable so no need to search
$a.attr('title', selAlt)
//if you need you can then extract that title attribute
var alt = $a.attr('title');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.