onClick 父 getElementById.click();无法在 Chrome 中工作

发布于 2024-10-17 19:07:23 字数 1648 浏览 4 评论 0 原文

我一直在尽力解决这个问题,但我真的找不到问题所在。我对 javascript 并没有真正的经验,所以这可能是我缺少的一些基本知识。

现在,我复制代码后发现允许IE以外的浏览器注册click();它在除 Chrome 之外的所有浏览器中都能完美运行。我测试了 IE、FF、Safari 和 Opera,它们都可以工作。

我的设置是,我有一个 iframe,其中带有按钮,这些按钮应该激活父级中的 Shadowbox 画廊。

我在主页 (index.html) 中的脚本是:

<head>

//shadowbox linking
<link rel="stylesheet" type="text/css" href="shadowbox/shadowbox.css">
<script type="text/javascript" src="shadowbox/shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init();
</script>

//included based on what I found online to enable the click(); method to work in browsers besides IE)
<script type="text/javascript">
if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt); }
</script>

</head>

<body>
<a id="web1" href="http://www.google.com/" rel="shadowbox[gallery]"></a>
<a id="web2" href="http://www.yahoo.com/" rel="shadowbox[gallery]"></a>

<iframe src="frame.html">You can't see
iframes.</iframe>
</body>

我在 iframe (frame.html) 中的链接是:

<a href="#" onClick="parent.document.getElementById('web1').click();">Link1</a>
<a href="#" onClick="parent.document.getElementById('web2').click();">Link2</a>

我真的希望有人可以帮助我 =) 谢谢!

I've been trying my best to solve this problem but I really can't find what's wrong. I'm not really experienced with javascript so it's probably something basic I'm missing.

Right now, after I copied the code I found to allow browsers besides IE to register click(); it works perfectly in every browser except for Chrome. I tested IE, FF, Safari, and Opera, they're all working.

My setup is that I have an iframe with buttons in it that are supposed to activate a shadowbox gallery in the parent.

My script in the main page (index.html) is:

<head>

//shadowbox linking
<link rel="stylesheet" type="text/css" href="shadowbox/shadowbox.css">
<script type="text/javascript" src="shadowbox/shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init();
</script>

//included based on what I found online to enable the click(); method to work in browsers besides IE)
<script type="text/javascript">
if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt); }
</script>

</head>

<body>
<a id="web1" href="http://www.google.com/" rel="shadowbox[gallery]"></a>
<a id="web2" href="http://www.yahoo.com/" rel="shadowbox[gallery]"></a>

<iframe src="frame.html">You can't see
iframes.</iframe>
</body>

My link in the iframe (frame.html) is:

<a href="#" onClick="parent.document.getElementById('web1').click();">Link1</a>
<a href="#" onClick="parent.document.getElementById('web2').click();">Link2</a>

I really hope someone can help me out =) Thanks!

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

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

发布评论

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

评论(2

鹿童谣 2024-10-24 19:07:23

哦,看来我发现了问题 -_-

脚本没有任何问题,它工作得很好。当文件托管在本地时,Chrome 似乎存在一些安全问题。我把文件上传到网上,一切正常。 =)

我希望这会对碰巧遇到它的人有所帮助,特别是从 iframe 链接 Shadowbox,因为我遇到了很多关于它的帖子。

Oh it seems I've found the problem -_-

There is nothing wrong with the script, it works perfectly fine. Chrome just seems to have some security issues when the files are hosted locally. I uploaded the files online and everything works fine. =)

I hope this will help someone who happens to come across it, especially linking Shadowbox from an iframe as I came across quite a few posts about it.

转身泪倾城 2024-10-24 19:07:23

Chrome 不支持(很好).click()。解决这个问题的一种方法是使用 jQuery。将 getElementById('web2').click() 转换为 $("#web2").click()

.click() is not supported (well) in Chrome. One way around it is to use jQuery. Convert getElementById('web2').click() to $("#web2").click().

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