2014-10-04 120 views
1

问题是,我已经在脚本jquery.min.js版本1.4上工作。必须升级到版本1.9.1后出现问题。Uncaught TypeError:无法读取未定义的属性'substr'

Uncaught TypeError: Can not read property 'substr' of undefined. 

告诉我该怎么做。 这部分发生错误的代码。

var processCaption = function(settings){ 
       var nivoCaption = $('.nivo-caption', slider); 
       if(vars.currentImage.attr('title') != ''){ 
        var title = vars.currentImage.attr('title'); 
        if(title.substr(0, 1) == '#') title = $(title).html(); 

        if(nivoCaption.css('display') == 'block'){ 
         nivoCaption.find('p').fadeOut(settings.animSpeed, function(){ 
          $(this).html(title); 
          $(this).fadeIn(settings.animSpeed); 
         }); 
        } else { 
         nivoCaption.find('p').html(title); 
        }     
        nivoCaption.fadeIn(settings.animSpeed); 
       } else { 
        nivoCaption.fadeOut(settings.animSpeed); 
       } 
      } 
+0

是什么瓦尔... – loveNoHate 2014-10-04 15:28:34

+0

之前尝试,如果(vars.currentImage.attr( '标题')){ – artm 2014-10-04 15:33:07

回答

2

尝试

Soultion

$.trim()将返回空字符串,如果它是undefined或者它只有whitespace

if($.trim($('#abc').attr('title')) != ''){ 

问题

如果vars.currentImage.attr('title')没有title属性将返回undefined没有''(空字符串)。而且你正在检查空字符串,所以它进入循环,如果它没有标题属性

所以你得到错误,因为substr只能应用于string

+1

不知道装饰功能恢复那 – 2017-11-10 02:52:15

1

检查空变量调用substr()

if (val1 != null) { 
    var val2= val1.substr(6); 
    //...... 
} 
相关问题