【问题标题】:Detecting clicks on an embedded webpage检测对嵌入式网页的点击
【发布时间】:2013-07-19 09:22:54
【问题描述】:

假设我在网站上有一个嵌入式网页(例如:www.en.wikipedia.org/wiki/Pulsar)。用户点击文章内的超链接(例如: https://en.wikipedia.org/wiki/Extrasolar_planet)。

当用户单击原始文章(“Pulsar”)中的条目时,我想要一个显示文章名称(在本例中为“系外行星”)的框架

我怎么知道用户点击了什么?

编辑:在看到许多示例后,我尝试这样做,但效果不佳。

<iframe width="940" height="550" src="http://en.wikipedia.org/wiki/Special:Random">    </iframe>


<script>
(document.getElementsByTagName('iframe').contentWindow.document).keydown = function() {
alert("Hello World")
})
</script>

【问题讨论】:

  • 您可以在 iframe 中附加事件侦听器,例如 this

标签: javascript html css ruby-on-rails


【解决方案1】:

这不起作用,因为您使用的是getElementsByTagName,它返回一个数组。请改用getElementByIdgetElementsByTagName('iframe')[0]

【讨论】:

  • 当用户单击嵌入式网站内的某些内容时,我希望收到警报,但我不明白。
【解决方案2】:

contentWindowiframewindow 对象。这里你想要iframedocument

$(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
    // my func
});

  $(document.getElementById('iframe_id').contentWindow.document).click(function() {
        // my func
    });

【讨论】:

  • 这是 Javascript 对吧?我已经嵌入了带有 HTML 对象代码的网页。
  • 是的,这是 javascript。您需要 javascript 来检测点击。正确的? HTML 永远无法做到这一点。我无法理解您在评论中提出的问题。
猜你喜欢
  • 2015-02-11
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 2021-09-24
相关资源
最近更新 更多