如何在点击时执行一个页面的javascript以触发另一页面上的点击事件?

发布于 2024-12-11 07:12:03 字数 527 浏览 0 评论 0原文

这就是第 1 页上的链接的样子:

<li id="click1">
  <a href="products.htm#test4Handle1">TNT Cable System</a>
</li>`

这就是我想要在第 2 页上触发的内容:

<a name="test4Handle1">
  <button onclick="$('#test4Handle1').click()">TNT Cable System</button>
</a>

我对 jquery 的尝试

$("test4Handle1").observe('domready',function() {
  document.getElementById("test4Handle1").click();
});

哪个页面应该包含 javascript 页面 1 或 2?

This is what the link looks like on page 1:

<li id="click1">
  <a href="products.htm#test4Handle1">TNT Cable System</a>
</li>`

This is what I want to trigger on page 2:

<a name="test4Handle1">
  <button onclick="$('#test4Handle1').click()">TNT Cable System</button>
</a>

my attempt at jquery

$("test4Handle1").observe('domready',function() {
  document.getElementById("test4Handle1").click();
});

What page should have the javascript page 1 or 2?

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

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

发布评论

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

评论(3

心房敞 2024-12-18 07:12:03

检查这是否对您有帮助。
以下是我工作的一个示例

,这里是 Page1.html

<!DOCTYPE html>
<html>
<head>
  <title>Page1</title>
</head>
<body>
  <h2>Page1</h2>
  <a href="page2.html?event=true">Click here to go on page2</a>
</body>
</html>

,这里是 Page2.html

<!DOCTYPE HTML>
<html>
<head>
  <title>Page2</title>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js" >   
  </script>
  <script type="text/javascript">
    $(function() { 
      var prevURL = (window.location.search).contains("event=true");
      if(prevURL == true)
        $("#btn_click").click();
      else
        alert("Not proper prev page");
     });
  </script>
</head>
<body>
  <h2>Page2</h2>
  <a href="page1.html">Click here to go on page1</a>

  <input type="button" id="btn_click" value="Test Button Click"/>
  <script>
    $("#btn_click").click(
       function() {
         alert("Button CLicked From jQuery");
       });
  </script>
</body>
</html>

Check if this helps you.
Following is one example I got working

Here is Page1.html

<!DOCTYPE html>
<html>
<head>
  <title>Page1</title>
</head>
<body>
  <h2>Page1</h2>
  <a href="page2.html?event=true">Click here to go on page2</a>
</body>
</html>

And here Page2.html

<!DOCTYPE HTML>
<html>
<head>
  <title>Page2</title>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js" >   
  </script>
  <script type="text/javascript">
    $(function() { 
      var prevURL = (window.location.search).contains("event=true");
      if(prevURL == true)
        $("#btn_click").click();
      else
        alert("Not proper prev page");
     });
  </script>
</head>
<body>
  <h2>Page2</h2>
  <a href="page1.html">Click here to go on page1</a>

  <input type="button" id="btn_click" value="Test Button Click"/>
  <script>
    $("#btn_click").click(
       function() {
         alert("Button CLicked From jQuery");
       });
  </script>
</body>
</html>
千仐 2024-12-18 07:12:03

在第 2 页上,粘贴此代码

$(document).ready(function(){
if(window.location.hash == "#test4Handle1"){
document.getElementById("test4Handle1").click();
}
});

On Page 2, paste this code

$(document).ready(function(){
if(window.location.hash == "#test4Handle1"){
document.getElementById("test4Handle1").click();
}
});
滥情稳全场 2024-12-18 07:12:03

如果我理解正确的话,我不得不说你正在寻找的东西是不可能的。脚本被限制在加载它们的页面上。虽然它们可以触发新窗口的创建,但不可能与这些窗口、它们的 DOM 以及它们正在运行的脚本进行交互。

If I understand you correctly, I would have to say that what you are looking for is not possible. Scripts are jailed to the pages on which they are loaded in. While it is possible for them to trigger the creation of new windows, it is not possible to interact with those windows, their DOM's and the scripts they are running.

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