简体   繁体   English

如何使用html锚链接到页面,然后在该页面上执行javascript函数?

[英]How can I use a html Anchor to link to page and then execute the javascript function on that page?

I've written a script that changes the content of a div in the main part of the html, using: 我编写了一个脚本,该脚本使用以下命令在html的主要部分中更改了div的内容:

document.getElementById("id").innerHTML = "the stuff I want to put in the main box";

and an array storing the content so I call changetxt(1) it will change the innerhtml to 'content[1]'etc.) 和一个存储内容的数组,所以我调用changetxt(1)它将把innerhtml更改为'content [1]'等。)

What I now want to do is link from another page including the changed content, so for example I link to the 'where we meet' content section, (content[2]) but when I link to the html it obviously just shows the default value, I was wondering if there is some way I can link to the content via an Anchor on another page such as - 我现在想做的是从包含更改内容的另一页面链接,例如,我链接到“我们遇到的地方”内容部分(content [2]),但是当我链接到html时,它显然只显示了默认内容值,我想知道是否可以通过某种方式通过另一页上的锚点链接到内容,例如-

href="index.html-javascript:changetxt(2); 

I've not found anything on google or this site explaining how to do so. 我没有在Google或此网站上找到任何说明该怎么做的内容。

many thanks 非常感谢

why not use hash tags? 为什么不使用哈希标签?

The link: 链接:

index.html#2

The javascript in the new page (index.html): 新页面(index.html)中的javascript:

if(window.location.hash) {
  var num = Number(window.location.hash.substr(1));

  if(num > 0) {
    changetxt(num);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM