Find & replace jquery

后端 未结 3 1875
感动是毒
感动是毒 2020-12-17 02:01

I have this code to give me a rollover on submit buttons, and I\'m trying to make it more generic:

$(\'.rollover\').hover(
            function(){ // Change          


        
相关标签:
3条回答
  • 2020-12-17 02:32
    $('.rollover').hover(
                function(){ // Change the input image's source when we "roll on"
                    var t = $(this);
                    t.attr('src',t.attr('src').replace(/([^.]*)\.(.*)/, "$1-over.$2"));
                },
                function(){ 
                    var t= $(this);
                    t.attr('src',t.attr('src').replace('-over',''));
                }
         );
    
    0 讨论(0)
  • 2020-12-17 02:34

    To manipulate the file name and append "-over" you simply have to do some Javascript string manipulation, like this:

    function appendOver(srcPath){
        var index = s.indexOf('.');
    
        var before = s.substr(0, index);
        var after = s.substr(index);
    
        return before + "-over" + after;
    }
    

    This should return the original filename (in all possible formats) and add the '-over' string just before the extension dot.

    0 讨论(0)
  • 2020-12-17 02:39

    You should be able to use a regex replace to modify your source path. Like this:

    srcPathOver = srcPath.replace(/([^.]*)\.(.*)/, "$1-over.$2");
    

    More on JavaScript regexes here

    As far as how you're doing it, I'd make sure that you define your srcPath variable like this

    var srcPath;
    $('.rollover').hover(...
    

    The code you have above makes it look like srcPath is a global variable, which is not what you want.

    0 讨论(0)
提交回复
热议问题