【问题标题】:If URL contains anchor link check matching checkbox如果 URL 包含锚链接检查匹配复选框
【发布时间】:2016-09-29 15:38:58
【问题描述】:

是否可以查看 URL 是否包含哈希锚链接

sitelink.com/page#example

然后勾选页面上的匹配复选框

<input type="checkbox" value="example" id="example">

【问题讨论】:

  • 你的意思是尖锐的标志? #

标签: jquery checkbox anchor


【解决方案1】:
//var url = 'sitelink.com/page#example';
var anchor = window.location.hash.substring(1);

$('input[type=checkbox][value="'+anchor +'"]').prop("checked",true);

【讨论】:

    【解决方案2】:

    window.location.hash 包含# 之后的片段加上# 本身

    只需将其用作选择器并设置checked 属性

    $(function() {
        $(window.location.hash).prop("checked", true);
    })
    

    【讨论】:

      【解决方案3】:
      var link = document.location.href.split('#');
      if(link.length==2)
      {
          var id = '#' + link[1];
          if($(id).length!=0)
               $(id).attr('checked',true);
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-12
        • 2014-02-24
        • 1970-01-01
        • 2014-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多