首页 > 解决方案 > 如何仅在特定页面或页面上运行 javascript 函数?

问题描述

我有以下功能,我只想在我的 Wordpress 网站的特定页面上执行它。我试过 if (is_page('pagename') 但我无法让它工作。有什么建议吗?

    $(function() {

        $('a[href*=\\#]:not([href=\\#])').click(function() {
            if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {

                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top
                    }, 2000);
                    return false;

                }

            }
        });

    });
}
})
;

标签: javascript

解决方案


像这样的东西会起作用:

if (window.location.pathname=='/account') {

}

推荐阅读