javascript - jquery检测url触发点击事件中的hash

标签 javascript jquery hash click

我看了很多关于 SO 的例子,但似乎看不出我的代码有什么问题:

我有一些 url 带有散列标签/跳转到附加到它们的 id 值。我想检查 url 是否有哈希标记,并将该值用作单击事件,就像我在单击按钮时在页面上所做的那样。

示例网址: http://domain.com/#help-video-modal-tags

j查询代码:

jQuery(document).ready(function() { 
    if ( window.location.hash ) {

        function showVideo() {
            alert('in the function');
        }

        jQuery(window.location.hash).click(showVideo);

}
});

当前点击事件触发函数

showVideo();

最佳答案

使用这个

jQuery(document).ready(function() { 
    if ( window.location.hash ) {

        function showVideo(hash) {
            alert('in the function, hash -> ' + hash);
        }

        jQuery(document).click(showVideo(window.location.hash));

    }
});

关于javascript - jquery检测url触发点击事件中的hash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23220679/

相关文章:

javascript - 服务器文件系统中的 XML 到 HTML 文本区域

javascript - 将 HTML 分成几个部分

javascript - Polymer dom-repeat 如何通知数组更新

jquery - 使用 window.resize (或其他方法)触发 jquery 函数并使 gridster.js 响应的有效方法

php - 当我使用加盐的 CRYPT_MD5 来加密我的密码时,加密的是什么?

c++ - const 和非常量类型的部分 std::hash 特化

ruby - 从 Ruby 中的 MongoDB 嵌套哈希中提取正确的字符串

Javascript中断页面请求

javascript - 更改 jqGrid 的样式

jquery - 每次选择器的父级有类(即事件)时进行检查,然后调用函数