【问题标题】:Do action in iFRame when people click in a specific part当人们单击特定部分时,在 iFRame 中执行操作
【发布时间】:2021-11-10 15:41:02
【问题描述】:

我有一个内部有 3 个按钮的 iframe,当人们单击 iframe 的特定部分时,我想删除此按钮,而不是单击按钮时,而是单击 iframe 上的特定部分时。当人们点击该部分时,这 3 个按钮必须消失。

这是代码:

<div class="frame-container">
<button class="iframe-button">
<img src="pic_trulli.jpg" alt="Clicca nello spazio verde per andare avanti->">
</button>
<button class="iframe-button2">
<img src="pic_trulli.jpg" alt="I trucchi sono aggiornati e disponibili GRATIS!">
</button>
<button class="iframe-button3">
<img src="pic_trulli.jpg" alt="Registrati adesso per riceverli via EMAIL!">
</button>
<div style="overflow: hidden; margin: -120px auto; max-width:;"><br>
<iframe scrolling="no" width="370" height="363" frameborder="0" target="_self" src="test.com" />
</iframe>
</div>
</div>

.iframe-button {
  position: absolute;
  top: 180px;
  left: 32px;
  background-color: #3366cc;
  border: none;
  color: white;
  padding: 21px 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 11px;
}
.iframe-button2 {
  position: absolute;
  top: 88px;
  left: 30px;
  background-color: #d9d9d9;
  border: none;
  color: #000000;
  padding: 9px 1px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 15px;
}
.iframe-button3 {
  position: absolute;
  top: 129px;
  left: 35px;
  background-color: #d9d9d9;
  border: none;
  color: #000000;
  padding: 11px 12.5px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 15px;
}

【问题讨论】:

  • 是同源的iframe源吗?
  • @HannaRose 按钮​​位于 iframe 内。我在 iFrame 中加载了 3 个按钮
  • 跨域资源共享 (CORS) 是一种基于 HTTP 标头的机制,它允许服务器指示任何来源(域、方案或端口)。因此,要访问 iframe 中的按钮,它必须是相同的来源。这就是为什么我问它是同一来源的 iframe 源?

标签: javascript html jquery css iframe


【解决方案1】:

@HannaRose 的问题很关键。

有两种情况:

  1. 您的 iframe 指向您可以控制的网站
  2. 您的 iframe 指向外部网站。

场景 1: 如果您的 iframe 指向您自己的网站,那么使用 iframe 可能不是最佳解决方案。 iframe 类似于单向窗口 - 当您与 iframe 后面的网站交互时,它不会返回任何数据或事件。 最好在页面上创建 3 个按钮来执行与 iframe 内的按钮相同的操作。

或者,在您的 iframe 指向的页面上 - 您可以在点击该页面时隐藏按钮。

场景 2: 如果您的 iframe 指向外部网站,您将根本无法隐藏按钮。如果可以,您将完全改变另一个网站的行为。 你能做的最好的就是在点击时隐藏整个 iframe

【讨论】:

  • 我有场景 2,但按钮是我自己在 iframe 中创建的。我只希望用户单击页面的一部分后创建的按钮消失。
  • 1.您的 iframe 指向您可以控制的网站 2. 您的 iframe 指向外部网站。我的意思是,是否相同。
  • 如果您处于方案 2 中,请阅读 CORS 政策。 developer.mozilla.org/en-US/docs/Web/HTTP/CORS
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 2018-08-04
相关资源
最近更新 更多