简体   繁体   English

为什么我的jquery变量在Firebug控制台中返回未定义的

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

相关问题 为什么控制台说我的函数/变量未定义? - Why does console say my function/variable is undefined? 为什么萤火虫说我的函数调用是“未定义的” - Why does firebug say my function call is “undefined” 为什么我的C#变量在Jquery中返回null? - Why does my C# variable return null in Jquery? 为什么我的jQuery-UI脚本不能在Greasemonkey中执行? 它在Firebug控制台中运行 - Why doesn't my jQuery-UI script execute in Greasemonkey? It runs in the Firebug console 为什么我的事件处理程序会导致“不是函数”错误,但是可以从Firebug控制台运行? - Why does my event handler cause an “is not a function” error, but works from the Firebug console? 为什么Alert返回一个“未定义”变量值? - Why does alert return an 'undefined' variable value? jQuery:为什么:包含选择器返回undefined? - jQuery: why does :contains selector return undefined? 将jQuery $ .support输入到FireBug控制台中返回的项目的清单是什么? - What are the list of items that entering jQuery $.support into FireBug console return? 为什么这个jQuery .post无法返回结果,我怎样才能在Firebug中调试? - Why does this jQuery .post fail to return a result, and how can I debug in Firebug? jquery 在萤火虫中未定义 - jquery post undefined in firebug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM