【问题标题】:Find the anchor element where the value is equal to the hash in the URL查找值等于 URL 中哈希值的锚元素
【发布时间】:2017-01-15 01:24:12
【问题描述】:

如何找到name= 值等于URL 中的哈希值的锚元素,然后将open 类添加到下一个div 元素?

if(window.location.hash) {
    var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
    console.log(hash);
    // Find the anchor element where the value is equal to the has in the URL, then add '.open' class to the next div element.
    $("a[name=$hash]").addClass('open'); //WRONG SYNTAX! How do I write this?
} 

【问题讨论】:

    标签: jquery hash find anchor addclass


    【解决方案1】:
    $(function() {
        if (location.hash === "#hash") {
            //Logic
         $("#divid").addClass('open');
        }
    });
    

    【讨论】:

    • 这个问题是值是硬编码的,我需要 #divid 是一个变量,例如hash
    • 我认为是$("a[name=$hash]).addClass('open');,但我知道这不是正确的语法。
    • 仅代码的答案对 SO 不适用,请添加说明
    • $hash 应该是hash
    • 赞这个$("a[name=" + hash + "]").next().addClass('open');
    猜你喜欢
    • 2014-12-18
    • 1970-01-01
    • 2016-10-31
    • 2014-12-18
    • 2012-07-02
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多