替换匹配函数提供了未定义的错误,但它适用于替换

时间:2016-04-08 20:13:03

标签: javascript jquery mobile responsive-design jquery-animate

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

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

我当前的错误说,无法获得未定义的顶部,这让我相信匹配或替换不起作用。

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

1 个答案:

答案 0 :(得分:0)

我知道了,只需要在执行子串之前执行替换功能。