Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery捕获页面链接_Javascript_Jquery_Html_Xhtml - Fatal编程技术网

Javascript 使用jQuery捕获页面链接

Javascript 使用jQuery捕获页面链接,javascript,jquery,html,xhtml,Javascript,Jquery,Html,Xhtml,嗨,我想在用户点击页面链接时触发一个函数,例如abc.com/hello.html#variable1我想捕捉#variable1并执行一个函数。要将逻辑附加到所有哈希链接,可以执行以下操作: $("a[href^='#']").click(function(e){ // user clicked an inpage link }); 如果要获取散列后的字符串: $("a[href*='#']").click(function() { var hash = this.href.re

嗨,我想在用户点击页面链接时触发一个函数,例如abc.com/hello.html#variable1我想捕捉#variable1并执行一个函数。

要将逻辑附加到所有哈希链接,可以执行以下操作:

$("a[href^='#']").click(function(e){
  // user clicked an inpage link
});

如果要获取散列后的字符串:

$("a[href*='#']").click(function() {
    var hash = this.href.replace(/.*#(.*)$/, '$1');
    // do something
    return false
});

捕获哈希并将其子字符串化:

$("a[href*='#']").click(function(e){
  var hash = $(this).attr('href').substring($(this).attr('href').indexOf("#"));
  //hash = #var
  function(hash);
});

如果要为动态插入哈希的链接触发函数,请使用以下命令:

$(document).click(function (event) {
    var target = $(event.target);
    if (target.filter("a[href*='#']").size() > 0) {
        var hash = target.attr("hash");

        // Do something with hash.

        event.preventDefault();
    }
});

这应该是:
var hash=This.href.replace(/^(.*)#(.*)$/,“$2”)