为了账号安全,请及时绑定邮箱和手机立即绑定

修改和扩展一个有效的 jquery 脚本

修改和扩展一个有效的 jquery 脚本

月关宝盒 2023-05-19 15:06:31
我有这个工作脚本:<script>jQuery( document ).ready(function() {    console.log( "ready!" );var hideUrl=['https://www.gustotosto.it/shop/boni/','https://www.gustotosto.it/shop/agropic/',];if(hideUrl.indexOf(window.location.href)>=0){    jQuery('.title-shop').hide();}});</script>现在我想修改它,因为它只是隐藏了 var 中列出的 url 的“.title-shop”;如果 url 包含 var hideUrl,我想扩展它并隐藏“.title-shop”,包括子页面。我这样修改了它,但它不起作用。怎么了?有人可以帮助我吗?<script>jQuery( document ).ready(function() {    console.log( "ready!" );var hideUrl=['https://www.gustotosto.it/shop/boni/','https://www.gustotosto.it/shop/agropic/',];if (window.location.href.indexOf("hideUrl") > -1){    jQuery('.title-shop').hide();}});</script>
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

您的代码中有两个问题。首先hideUrl是一个变量名,所以你不应该用引号引起来。这样做意味着它被视为字符串文字。


其次,您需要遍历hideUrl数组中的所有项目并单独比较它们。尝试这个:


jQuery($ => {

  var hideUrls = [

    'https://www.gustotosto.it/shop/boni/',

    'https://www.gustotosto.it/shop/agropic/'

  ];


  hideUrls.forEach(url => {

    if (window.location.href.indexOf(url) > -1) {

      $('.title-shop').hide();

    }

  });

});


查看完整回答
反对 回复 2023-05-19
  • 1 回答
  • 0 关注
  • 96 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信