javascript - 如何仅在特定页面或页面上运行 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;
}
}
});
});
}
})
;
解决方案
像这样的东西会起作用:
if (window.location.pathname=='/account') {
}
推荐阅读
- angular - 如何在 html 视图中显示表单控件的值?
- javascript - PHP 按位和 Javascript 负数
- javascript - @ 在导入路径中是什么意思?
- python - 使用请求库将 CURL 请求转换为 Python
- redis - 为什么使用 spark-submit 将 4000 张图片加载到 redis 需要的时间(9 分钟)比将相同的图片加载到 HBase(2.5 分钟)要长?
- python - 使用语言环境打印以色列货币
- asp.net-core - 通过业务层在表示层初始化数据库上下文(域层)
- c# - c#身份无法创建自定义属性
- html - css模板布局怎么做?
- mongodb - MongoDB按名称计算数组中的项目