TypeError: undefined is not an object error
TypeError: undefined is not an object error
我有这段代码,它抛出一个 TypeError。我找不到问题所在。
代码
/ Topic Banners - swap out mobile ads
if ($('#advertisment-main').length){
if (matchMedia('(min-width: 768px)').matches) {
var topicBannerName = $('.topic-banner[src*="mobile"]').attr('src').replace('mobile.jpg','');
$('.topic-banner').attr('src', topicBannerName + 'tablet.jpg');
} else{
var topicBannerName = $('.topic-banner[src*="tablet"]').attr('src').replace('tablet.jpg','');
$('.topic-banner').attr('src', topicBannerName + 'mobile.jpg');
}
}
错误
TypeError: undefined is not an object (evaluating '$('.topic-banner[src*="tablet"]').attr("src").replace')
那是因为您的选择器 $('.topic-banner[src*="tablet"]')
不匹配任何元素。确保选择器和 HTML 代码正确。
我有这段代码,它抛出一个 TypeError。我找不到问题所在。
代码
/ Topic Banners - swap out mobile ads
if ($('#advertisment-main').length){
if (matchMedia('(min-width: 768px)').matches) {
var topicBannerName = $('.topic-banner[src*="mobile"]').attr('src').replace('mobile.jpg','');
$('.topic-banner').attr('src', topicBannerName + 'tablet.jpg');
} else{
var topicBannerName = $('.topic-banner[src*="tablet"]').attr('src').replace('tablet.jpg','');
$('.topic-banner').attr('src', topicBannerName + 'mobile.jpg');
}
}
错误
TypeError: undefined is not an object (evaluating '$('.topic-banner[src*="tablet"]').attr("src").replace')
那是因为您的选择器 $('.topic-banner[src*="tablet"]')
不匹配任何元素。确保选择器和 HTML 代码正确。