最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Uncaught TypeError: Cannot read property 'substr' of undefined - Stack Overflow

matteradmin2PV0评论

The problem is, I have worked on the script jquery.min.js version 1.4. After had to upgrade to version 1.9.1, and there was a problem.

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

Tell me what to do. This part of the code in which the error occurred.

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);
                }
            }

The problem is, I have worked on the script jquery.min.js version 1.4. After had to upgrade to version 1.9.1, and there was a problem.

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

Tell me what to do. This part of the code in which the error occurred.

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);
                }
            }
Share Improve this question asked Oct 4, 2014 at 15:26 NikitaNikita 231 gold badge1 silver badge3 bronze badges 1
  • Try if(vars.currentImage.attr('title')){ – artm Commented Oct 4, 2014 at 15:33
Add a ment  | 

2 Answers 2

Reset to default 3

Try

Soultion

$.trim() will return empty string if it is undefined or it has only whitespace .

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

Problem

if vars.currentImage.attr('title') doesn't have title attribute will return undefined not '' (empty string) . And you are checking for empty string so it goes inside the loop if it doesn't have title attribute

So you get error because substr can only be applied to string.

check the variable for null before calling substr()

if (val1 != null) {
     var val2= val1.substr(6);
     //......
}
Post a comment

comment list (0)

  1. No comments so far