Javascript 如何在点击时始终获得正确的链接

Javascript 如何在点击时始终获得正确的链接,javascript,jquery,html,Javascript,Jquery,Html,如果在“i”标记类上单击下面的任何链接,整个“span”标记将被记录到控制台,包括整个“a”本身 我的问题是,每次我点击下面的任何一个链接时,有时登录到控制台的信息都是“未定义”的,就像链接不存在一样 如何使它在每次单击时始终记录正确的信息。(因此,它应该记录“”标记、“”标记的类和“”标记。) //用于处理以上html链接上的单击的javascript $(".mood-list").click(function(e){ // get the appropriate link tha

如果在“i”标记类上单击下面的任何链接,整个“span”标记将被记录到控制台,包括整个“a”本身

我的问题是,每次我点击下面的任何一个链接时,有时登录到控制台的信息都是“未定义”的,就像链接不存在一样

如何使它在每次单击时始终记录正确的信息。(因此,它应该记录“”标记、“”标记的类和“”标记。)


//用于处理以上html链接上的单击的javascript

$(".mood-list").click(function(e){
   // get the appropriate link that was 
   // clicked and store it in the variable
   // mood
   var mood =  e.target;
    //sometimes when a particular link is clicked this 
   // will log as "undefined"
    console.log(mood);
//find the icon and get it's class    
var icon = $(mood).find("i");
    if(icon != "null" || icon != "undefined"){
   // find the "<i></i>" tag and get the class  
   icon = $(icon).attr("class");

     //sometimes when a particular link is clicked this 
   // will also log as "undefined"  
    console.log(icon);
    }
 var span = $(mood).find("span")[0];        
   if(span != "null" || span != "undefined"){
     //find the span element and gets html
    //sometimes when a particular link is clicked this 
   // will also log as "undefined"  
console.log(span);
   }

});
$(“.mood list”)。单击(函数(e){
//获取所创建的相应链接
//单击并将其存储在变量中
//心情
var=e.target;
//有时,当单击某个特定链接时
//将记录为“未定义”
console.log(情绪);
//找到图标并获取其类别
var icon=$(mood.find(“i”);
如果(图标!=“null”| |图标!=“未定义”){
//找到“”标记并获取类
icon=$(icon.attr(“类”);
//有时,当单击某个特定链接时
//也将记录为“未定义”
console.log(图标);
}
var span=$(mood.find(“span”)[0];
if(span!=“null”| | span!=“未定义”){
//找到span元素并获取html
//有时,当单击某个特定链接时
//也将记录为“未定义”
控制台日志(span);
}
});

您的主要问题是您的值比较。您不应该比较字符串值
“null”
“undefined”
,而应该比较实际的
null
undefined
值。但是,在这种情况下,由于jQuery返回一个列表(即使没有找到任何元素),
if($(mood.find(“i”).length){…}
if($(语气).find(“span”).length){…}

这将确保您的“已找到”和“未找到”检查准确无误,这意味着您不会试图在最初不存在的元素上查找子元素(即
.find(“i”)
.find(“span”)

下面是一个如何工作的快速示例:

$(“.mood list”)。在(“单击”上,函数(){
var$trigger=$(此);
var$icon=$trigger.find(“i”);//将返回项目的jQuery列表(list.length==0表示未找到)
var$span=$trigger.find(“span”);//将返回项的jQuery列表(list.length==0表示未找到)
log($trigger.get(0));
如果($icon.length){
让iconClass=$icon.attr(“类”);
控制台日志(iconClass);
}
否则{
console.log(“未找到图标”);
}
如果($span.length){
log($span.html());
}
否则{
console.log(“未找到范围”);
}
});
。情绪列表{
显示:块;
}


你有问题吗?我认为问题在于你的值比较。你不应该比较字符串值
“null”
“undefined”
,而应该比较实际的
null
undefined
值。或者更简单地说,
if($(mood)。find(“I”).length){…}
if($(mood)。find(“span”).length){…}
在旁注中,
myVar!=null
myVar!=undefined
实际上是相同的检查,因为您正在比较强制相等,而
null
undefined
强制相等。@mhodges,非常感谢,但这只会解决比较问题。有或没有比较,unde的问题被记录仍然会持续。我的问题是我想在每次创建任何一个链接时,将“”标记的类、整个“span”标记和整个“a”标记记录到控制台clicked@Abcaha在下面检查我的答案,看看这对你是否有效you@Abcaha很棒的
:)
很高兴我能提供帮助。这有助于澄清您的原始代码不起作用的原因吗?@Abcaha另外,如果我的解决方案最终对您有效,请确保将我的答案标记为已接受。=)
$(".mood-list").click(function(e){
   // get the appropriate link that was 
   // clicked and store it in the variable
   // mood
   var mood =  e.target;
    //sometimes when a particular link is clicked this 
   // will log as "undefined"
    console.log(mood);
//find the icon and get it's class    
var icon = $(mood).find("i");
    if(icon != "null" || icon != "undefined"){
   // find the "<i></i>" tag and get the class  
   icon = $(icon).attr("class");

     //sometimes when a particular link is clicked this 
   // will also log as "undefined"  
    console.log(icon);
    }
 var span = $(mood).find("span")[0];        
   if(span != "null" || span != "undefined"){
     //find the span element and gets html
    //sometimes when a particular link is clicked this 
   // will also log as "undefined"  
console.log(span);
   }

});