javascript - 在 div 加载页面内的 onClick

标签 javascript jquery html jquery-events

我在一个 div 中加载一个页面。这个 div 应该在单击鼠标时触发一个函数。我希望这个 onClick 事件在您点击加载的页面时也有效。

如果您在 div 中加载页面,则加载页面的功能仅适用于该页面。 我怎样才能使加载的页面也触发 onClick 函数?

这是一个简单的代码示例:

function load() {
  var url_view = '<object type="text/html" data="http://www.example.com/"></object>';
  document.getElementById("image").innerHTML=url_view;
}
window.onload = load;
  function showbox() {
    alert("test");
  }
<div id ="frame"  onclick="showbox()">
  <div id="image">
  </div>
  <div class="infotext">
    <span>here comes some text.</span>
  </div>
</div>

说清楚
我想要实现的是,当您加载页面时,您将从 example.com 加载数据到 ID 为“image”的 div 中。此事件发生在 window.load 上的函数“load()”上。

准备就绪后,我希望您在单击 inside id 为“image”的 div(example.com 加载到的 div ).

最佳答案

你可以尝试这样的事情:

<div id ="frame">
  <div id="image">
  </div>
  <div class="infotext">
    <span>here comes some text.</span>
  </div>
</div>

JS:

function showbox() {
  alert("show box");
}

$(document).ready(function() {

  $.get('https://jsfiddle.net/about', function(data) {
    $('#image').html(data);
    showbox();
  });

  $('#image').click(function() {
    showbox();
  });
});

由于 CORS,在 jsfiddle 上运行它:https://jsfiddle.net/usn9qam7/1/

关于javascript - 在 div 加载页面内的 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308181/

相关文章:

javascript - 使用 HashMap 分配声音

javascript - 基于欧芹验证显示 JqueryUI 工具提示

jquery - magento 上的 fancybox 混合内容问题

javascript - 使用 Kendo Mobile,如何防止 View 滚动?

html - flexbox 元素不会到达容器的末尾

javascript - 关于在 JavaScript 中调用函数,哪一项是正确的?

javascript - 尝试了所有 jquery 方法和 js 来检查文件是否存在,但都不起作用

javascript - 从变量中获取 `/`之后的值

jquery - 使用 jquery jsonp 访问 Coursera api 中的数据

jquery - 悬停/选定的菜单显示背景