简体   繁体   English

jQuery在console.log中工作,但在变量中工作

[英]jQuery working in console.log but not in variable

I have this console.log() 我有这个console.log()

console.log($($('.biggie img:visible')[0]).attr('src').split('/').pop());

This returns: 返回:

4.jpg

When i put the line into a variable like this: 当我将行放入这样的变量中时:

var jpg = $($('.biggie img:visible')[0]).attr('src').split('/').pop(); 

The console says: 控制台说:

Uncaught TypeError: Cannot call method 'split' of undefined 

What is going on? 到底是怎么回事?

Thank you!! 谢谢!!

Edit: 编辑:

Sorry, sorry, I know what is going on now, you pointed me all into the right direction below my answer...here is the whole code: 对不起,对不起,我知道现在发生了什么,您在我的答案下方将我全都指向正确的方向……这是整个代码:

imgVis.fadeOut(400, function() {
                var jpg = $($('.biggie img:visible')[0]).attr('src').split('/').pop(),
                    nr = parseInt(jpg,10);
                $(this).next(':hidden').fadeIn(400);
                console.log($($('.biggie img:visible')[0]).attr('src').split('/').pop());
});

I actually had no image visible. 我实际上没有图像可见。 Ashaming. ham愧。 :/ Thank you!!! :/ 谢谢!!!

Hm ... this is really strange. 嗯...这真的很奇怪。 Are these two lines one after each other. 这两条线是一个接一个的吗? If not, then I'll suggest that your selector is not matching what you expect or there is no src attribute of the matched elements. 如果不是,那么我建议您的选择器与您期望的不匹配,或者匹配的元素没有src属性。 I'll suggest two things: 我将建议两件事:

1) Replace this 1)更换

$($('.biggie img:visible')[0])

with just 只是

$('.biggie img:visible')

What you are doing there is to convert the jQuery element to a DOM element and the again make it jQuery element. 您正在执行的操作是将jQuery元素转换为DOM元素,然后再次将其变为jQuery元素。

2) console.log($('.biggie img:visible').length); 2) console.log($('.biggie img:visible').length);

If the length is 0 , then there are still no visible images. 如果长度为0 ,则仍然没有可见的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM