替换匹配函数给出了未定义的错误,但它与替换一起工作

Replace Match Function is giving undefined error, but it works with replace

本文关键字:替换 工作 一起 错误 未定义 函数      更新时间:2024-04-11

我有链接,它们应该滚动到正确的部分,但是,除非我首先更改它们的id,否则主主题标题将不起作用。一旦我这么做了,它就不再滚动了。

这里有一个网站链接(必须将浏览器设置为780px以下)http://lookupblue.com/elclimo,请注意,如果您选择婚礼之类的副标题,整个页面将滚动,但如果您选择活动,则不会滚动。这是因为id#Events正被其他东西使用,所以在滚动到该位置之前,我需要将#Events替换为#Section1。

我目前的错误是,无法找到未定义的顶部,这让我相信匹配或替换不起作用。

       <script type="text/javascript">
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
                navigator.userAgent)) {
            function backupNavigation_click(clicked_href)
            {
                var abc = clicked_href.substring(clicked_href.lastIndexOf('/') +
                    1);
                if (abc.match("^#")) {
                    $('a').on("click", function(e) {
                        // prevent default state change
                        e.preventDefault();
                        // get href of clicked
                        var abc = $(this).attr('href');
                        // Find last '/' and get everything after
                        abc = abc.substring(abc.lastIndexOf('/') + 1,
                            abc.length);
                        // If '#' found then chop off
                        if (abc.indexOf('#') > -1) {
                            abc = abc.substring(1, abc.length);
                        }

var mapObj = {
   Home:"Section0",
   Events:"Section1",
   Vehicles:"Section2",
Testimonials:"Section3",
Specials:"Section4",
AboutUs:"Section5",
Quotes:"Section6"
};
 abc = abc.replace(/Home|Events|Vehicles|Testimonials|Specials|AboutUs|Quotes/gi, function(matched){
  return mapObj[matched];
});


                        console.log(abc);

                        // Find matching with matching id
                        var pos = $('#' + abc).offset().top;
                        console.log(pos);
                        // Scroll to it
                        $('html, body').animate({
                            scrollTop: pos
                        }, 400);
                    })
                } else {
                    $('a').on("click", function(e) {
                        // prevent default state change
                        e.preventDefault();
                        // get href of clicked
                        var abc = $(this).attr('href');
                        // Find last '/' and get everything after
                        abc = abc.substring(abc.lastIndexOf('/') + 1,
                            abc.length);
                        // If '#' found then chop off
                        if (abc.indexOf('#') > -1) {
                            abc = abc.substring(1, abc.length);
                        }
                        console.log(abc);
                        // Find matching with matching id
                        var pos = $('#' + abc).offset().top;
                        console.log(pos);
                        // Scroll to it
                        $('html, body').animate({
                            scrollTop: pos
                        }, 400);
                    })
                }
            }
        } else {
            if (window.innerWidth > 779) {
                $('head').append(
                    '   <script type="text/javascript" src="js/jquery.fullPage.js">'
                );
            }
            $(document).ready(function() {
                $('#fullpage').fullpage({
                    anchors: ['Home', 'Events', 'Vehicles',
                        'Testimonials', 'Specials',
                        'AboutUs', 'Quotes'
                    ],
                    sectionsColor: ['none', 'none', 'none',
                        'none', 'none', 'none', 'none'
                    ],
                    scrollOverflow: true
                });
            });
        }
    </script>

工作的替换代码看起来像:

var str = "I have a cat, a dog, and a goat.";
var mapObj = {
   cat:"dog",
   dog:"goat",
   goat:"cat"
};
str = str.replace(/cat|dog|goat/gi, function(matched){
  return mapObj[matched];
});
alert(str);

我得到了它,只需要在做子字符串之前做替换函数。