gpt4 book ai didi

jQuery 触发 iFrame 上的点击?

转载 作者:行者123 更新时间:2023-12-03 22:17:29 25 4
gpt4 key购买 nike

如果用户单击图像,我希望它同时触发对指定 iFrame 的单击。那可能吗?图像和 iFrame 位于同一页面上。如果是这样,您能告诉我可行的代码吗?

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

<!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图像选择器是错误的。

改变

jQuery("input.image")

$("#image")

然后一切都会正常。你可以在这个fiddle中看到它

帖子编辑

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

$("#iframe").click()

类似:

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

只有当主页和 iFrame 页面的内容位于同一域时,此功能才有效。否则浏览器将阻止该操作,因为它是跨站点脚本。您可以在示例中看到,XSS 预防将会发生,如 this fiddle 中所示。

关于jQuery 触发 iFrame 上的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16928860/

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