我在一个 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/