jQuery触发对iFrame的点击? [英] jQuery to trigger a click on an iFrame?

查看:81
本文介绍了jQuery触发对iFrame的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户单击图像,我也希望该图像同时触发对指定iFrame的单击.那可能吗?图像和iFrame位于同一页面上.如果是这样,您能给我看看能用的代码吗?

If a user clicks on an image I want that to simultaneously trigger a click on a specified iFrame as well. Is that possible? The image and iFrame are located on the same page. If so, can you please show me the code that would work?

这是我到目前为止所拥有的:

Here's what I have so far:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<img id="image" src="https://www.google.com/images/srpr/logo4w.png">

<iframe id="iframe" src="http://www.test.com" height= "100" width="160"></iframe>

<script>
jQuery("input.image").click(function(){
   $("#iframe").click()
});
</script> 

</body>
</html>

推荐答案

您的jquery图片选择器错误.

Your jquery image selector is wrong.

更改

jQuery("input.image")

$("#image")

然后一切正常.您可以在此小提琴

Then everything will work. You can see it in this fiddle

发布修改

如果问题是触发对iFrame中包含的元素之一的点击,请替换

If the question is to trigger a click on one of the elements contained in the iFrame, then replace the

$("#iframe").click()

类似:

$("#iframe").contents().find("a:first").click();

仅当主页和iFrame页面的内容位于同一域中时,此选项才有效.否则,浏览器将阻止该操作,因为它是跨站点脚本编写的.您可以在示例中看到,如<​​a href="http://jsfiddle.net/J3Srw/1/">此小提琴

This will only work if the contents of the main page and the iFrame's page are on the same domain. Otherwise browsers will prevent the action as it is cross site scripting. You can see in your example, XSS prevention will occur as demonstrated in this fiddle

这篇关于jQuery触发对iFrame的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆