gpt4 book ai didi

javascript - 单击 anchor 元素后执行操作

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:47 25 4
gpt4 key购买 nike

我正在让一个网页在占据整个屏幕的 iframe 中加载另一个网页。我有 2 个 div,它们彼此重叠。这些 div 实际上是指向新 url 的 anchor (一个 Action )。我想要做的是,一旦单击了任何一个 div,就会启动一个 onclick 事件,该事件将转到该 anchor href 中指定的 url,然后重新加载初始 index.html 页面。我的类(class)正在研究本地环境,教我们一种叫做点击劫持的东西。以下是我当前的 html。

我遇到的问题是我可以让 anchor 击转到我想要的 url,但我不再在我的 index.html 中。是否可以在新窗口中打开引用的链接,加载后关闭新窗口,然后刷新 index.html 页面。完成后隐藏被点击的 div,因为“第二个”div 隐藏在最可点击的 div 后面。

<html> 
<head>
</head>
<body>
<center>
<!-- Two anchor divs that are clickable and on top of each other -->
<div id="secondDiv">
<a href="http://www.2ndURL.com" id="m2">
<div id="2" style="position:absolute;top:195px;left:10px;width:10000px;height:200px;">
<iframe>
</iframe>
</div>
</a>
</div>
<div id="firstDiv">
<a href="#" id="m1" onclick="myFunction(); return false;">
<div id="1" style="position:absolute;top:195px;left:10px;width:10000px;height:200px;">
<iframe>
</iframe>
</div>
</a>
<!-- Main iFrame where user interacts -->
</div>
<iframe id="new" src="http://www.mywebpage.com" style="opacity:1.0;width:100%;height:100%;">
</iframe>
</center>
</body>
<script>
function myFunction(){
var newWindow = window.open("http://www.actionURL");
newWindow.close()
var myWindow = window.open("file:///home/seed/Desktop/index.html", "_self");

}
</script>
</script>
</html>

简单描述:

  • 在网页中加载 iframe
  • 点击指向新 url 的页面上的 anchor div
  • 加载该 url,加载后返回 index.html(全部在一个选项卡中)(第 1 步)
  • 隐藏选定的 anchor div——允许第二个 anchor 点击

感谢您的帮助。

最佳答案

如果您希望在您所在的页面上执行 onclick 操作,您可能需要“伪造”onclick。您将需要修改参数,但这是我使用的代码:

    $(function() {
// find out what our query parameters contain.
var query = location.search;

// isolate the position of the equal sign.
var equalIndex = query.indexOf('=');
// extract the image's id.
var imageId = query.substring(equalIndex + 1);

// click on the appropriate image.
$('#' + imageId).click();
});

注释解释了代码的每一步执行的操作。在我的例子中,我需要加载页面,然后使用唯一的图像 ID 强制点击。在您的情况下,您需要将其绑定(bind)到您独特的 div。我希望这可以帮助您入门。

关于javascript - 单击 anchor 元素后执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33712213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com