简体   繁体   English

单击链接后执行操作

[英]Perform an action after clicking on the link

I'm new to JavaScript, I need to perform an action after clicking on the link.我是 JavaScript 的新手,我需要在点击链接后执行一个操作。 For example, we have two pages A and B On page A, I perform a JavaScript action (filling in the text field and clicking on the button), there is a transition to page B. Here, too, you need to perform some JavaScript action, such as clicking on a link.例如,我们有两个页面A和B 在页面A上,我执行了一个JavaScript的动作(填写文本字段并点击按钮),有一个到页面B的转换。这里也需要执行一些JavaScript动作,例如点击链接。 Here is an example that only works on page A, when the transition occurs, the rest of the code seems to be erased, this is not a fact, but my guesses这是一个只在页面A上有效的例子,当发生转换时,代码的rest似乎被删除了,这不是事实,但我的猜测

document.getElementById('story').value='Титаник';
document.querySelector('body > div.wrapper > div.header > div.header44 > div.search_panel > span > form > button').click();
document.getElementsByTagName('a')[1].click();

Or another example, Follow the link and fill in the field there, which also does not work或者另一个例子,点击链接并在那里填写字段,这也不起作用

window.location.href='http://www.ganjawars.ru/login.php'
function F() {
document.getElementsByName('login')[0].value = 'login';
document.getElementsByName('pass')[0].value = 'password';
}
 onload= F ()

How do I perform an action after clicking on a link?单击链接后如何执行操作?

Unfortunately, JavaScript does not work like this.不幸的是,JavaScript 不是这样工作的。 The variables that you store on page A, will be gone, when you navigate to page B.当您导航到页面 B 时,您存储在页面 A 上的变量将会消失。

Possible solutions:可能的解决方案:

  • One-page website一页网站
  • jQuery post the variables to another page. jQuery 将变量发布到另一个页面。
  • PHP Sessions PHP 场次
  • Cookies Cookies

Session storage is actually the easiest solution, providing that you only want to navigate to a different page on the same website. Session 存储实际上是最简单的解决方案,前提是您只想导航到同一网站上的不同页面。 This breaks when you close the browser or navigate to a new website.当您关闭浏览器或导航到新网站时,这会中断。

story = document.getElementById('story').value='Титаник';
sessionStorage.setItem("story", story);
document.getElementsByTagName('a')[1].click();

and on the other page:在另一页上:

story = sessionStorage.getItem("story");

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

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