【问题标题】:How do I check if an element is visible and check if the URL contains a string using JQuery?如何检查元素是否可见并使用 JQuery 检查 URL 是否包含字符串?
【发布时间】:2017-06-12 10:35:25
【问题描述】:

所以我有一个包含 #gallery-carousel-3 的 URL。当用户单击右侧图标 (.icon.icon-arrow-right) 时,将添加 .blockd 类。这很好用。但是,当用户单击左侧图标(.icon.icon-arrow-left)时,如果 URL 包含 #gallery-carousel-3 并且 .panel-display div 可见,我想删除 .blockd 类。我似乎无法让第二部分工作

我的代码如下:

      jQuery(document).ready( function($) {

            $('.icon.icon-arrow-right').click(function(){

              if(window.location.href.indexOf("#gallery-carousel-3") > -1 && $('.ad-panel').filter(':visible').length == 0 ) {
                $('.owl-wrapper-outer').addClass('blockd');

              }         

            });

            $('.icon.icon-arrow-left').click(function(){


              if(window.location.href.indexOf("#gallery-carousel-3") > -1 && $('.panel-display').is(':visible')) {
                $('.owl-wrapper-outer').removeClass('blockd');
              }            


            });
      });  

【问题讨论】:

  • 能否在if 条件if(window.location.href.indexOf("#gallery-carousel-3") > -1 && $('.panel-display').is(':visible')) 之前添加一条警告语句alert($('.panel-display').is(':visible')) 以验证.panel-display 确实可见?
  • 是的,我要检查一下。

标签: javascript jquery


【解决方案1】:

您可以使用:location.hash 获取 url 中哈希 (#) 的值

所以,假设你的网址是http://example.com/#gallery-carousel-3

var urlHash = location.hash; // => #gallery-carousel-3

【讨论】:

  • 我理解并且 location.hash 返回位置。我试图弄清楚为什么 if(window.location.href.indexOf("#gallery-carousel-3") > -1 && $('.panel-display').is(':visible')) 不能正常工作
  • 啊,然后打开你的 chrome 开发工具并检查 if() 的哪一部分没有通过。我怀疑$('.panel-display').is(':visible') 不是真的
  • 好的,我去看看。这让我走上了正确的道路
猜你喜欢
  • 1970-01-01
  • 2012-03-28
  • 2017-11-20
  • 2019-10-22
  • 2012-08-02
  • 1970-01-01
  • 2017-01-01
  • 2017-09-23
  • 2013-09-17
相关资源
最近更新 更多