是否可以从锚标记调用 Javascript?

发布于 2024-12-10 15:47:32 字数 292 浏览 0 评论 0原文

可能的重复:
在js中处理URL锚点更改事件

是否可以有一个javascript函数通过锚标记运行。

例如,如果您访问... www.site.com/index.html#function1 ... 那么 function1 会运行吗?

Possible Duplicate:
Handle URL anchor change event in js

Is it possible to have a javascript function run via an anchor tag.

E.g. if you went to... www.site.com/index.html#function1 ... Then function1 would run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏有森光若流苏 2024-12-17 15:47:32

在页面上放置一个 onload 侦听器,并在指向锚点的链接上放置一个单击侦听器。触发后,检查 window.location.href,如下所示:

<body onload="checkHash();">

  <a href="#foo" onclick="checkHash()">go to foo</a>
  <br>
  <a href="#bar">go to bar</a>

  <p>something in between</p>

  <a name="foo">foo</a>
  <br>
  <a name="bar">bar</a>

<script type="text/javascript">
  function checkHash() {
    // Check URL using setTimeout as it may not change before
    // listener is called
    window.setTimeout(doHashCheck, 10)
  }
  var doHashCheck = (function(global) {
    return function() {
      var fnName = window.location.hash.replace(/^#/,'');
      console.log(fnName);
      // fnName should be a native function, not a host method
      if (typeof global[fnName] == 'function') {
          global[fnName]();
      }
    }
  })(this);

  function foo() {
    alert('foo');
  }

  if (!window.console) window.console = {};
  if (!console.log) console.log = window.alert;
</script>
</body>

Put a an onload listener on the page and a click listener on the link that goes to the anchor. When fired, check window.location.href, something like:

<body onload="checkHash();">

  <a href="#foo" onclick="checkHash()">go to foo</a>
  <br>
  <a href="#bar">go to bar</a>

  <p>something in between</p>

  <a name="foo">foo</a>
  <br>
  <a name="bar">bar</a>

<script type="text/javascript">
  function checkHash() {
    // Check URL using setTimeout as it may not change before
    // listener is called
    window.setTimeout(doHashCheck, 10)
  }
  var doHashCheck = (function(global) {
    return function() {
      var fnName = window.location.hash.replace(/^#/,'');
      console.log(fnName);
      // fnName should be a native function, not a host method
      if (typeof global[fnName] == 'function') {
          global[fnName]();
      }
    }
  })(this);

  function foo() {
    alert('foo');
  }

  if (!window.console) window.console = {};
  if (!console.log) console.log = window.alert;
</script>
</body>
梦亿 2024-12-17 15:47:32

您不能通过简单地将其包含在锚标记中来做到这一点,但您可以使用主题标签 (location.hash) 来调用您的函数

You can't do that by simply including it in the anchor tag but you could use the hashtag (location.hash) in order to call your function

萤火眠眠 2024-12-17 15:47:32
    $(document).ready(function() {

        //Get hash from URL. Ex. index.html#Chapter1
        var hash = location.hash;

        if (hash == '#Chapter1') {
            //Do stuff here
        }
    });
    $(document).ready(function() {

        //Get hash from URL. Ex. index.html#Chapter1
        var hash = location.hash;

        if (hash == '#Chapter1') {
            //Do stuff here
        }
    });
远昼 2024-12-17 15:47:32

你不能像这样调用javascript,但是如果你控制其他站点,你可以检查正文Load事件中的URL,然后执行脚本。

You can't call the javascript like that, but if you control the other site, you can check the URL in the body Load event, then execute a script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文