Javascript 如何制作div加载功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8500185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:16:01  来源:igfitidea点击:

How to make a div onload function?

javascript

提问by Jenny

I want to load an HTML page as a div inside my webpage by removing its HTML and body tags. But the HTML page has a <body onload=" ... " >, I need this function to continue working. Seems <div onload=" ... " >is not working. How can I insert this onload function into my website's body (on this page only) without directly editing my original website code (php)?

我想通过删除 HTML 和 body 标签来将 HTML 页面作为 div 加载到我的网页中。但是 HTML 页面有一个<body onload=" ... " >,我需要这个功能才能继续工作。似乎<div onload=" ... " >不起作用。如何在不直接编辑我的原始网站代码 (php) 的情况下将此 onload 函数插入我的网站正文(仅在此页面上)?

采纳答案by Jason

you can use jQuery.loadto load the contents of the page into your div

您可以使用jQuery.load将页面内容加载到您的 div 中

$(document).ready(function() {
  $("#containing-div").load("[url of page with onload function]");
});

the code above goes in the page that contains the div. the page with the onload function doesn't get changed.

上面的代码进入包含 div 的页面。带有 onload 功能的页面不会改变。

回答by Matt Cashatt

Have you used jQuery before? If so, just get the id of your div (let's say "SomeDiv") and then use the "ready" method like this:

你以前用过jQuery吗?如果是这样,只需获取您的 div 的 id(假设为“SomeDiv”),然后使用“ready”方法,如下所示:

$("#SomeDiv").ready(
function(){
     //do stuff
    });

回答by Roman

You can add an additional Javascript tag at the end of the loaded page (once inserted inside the div) which will executing as soon as it's loaded. Like this:

您可以在加载页面的末尾添加一个额外的 Javascript 标记(一旦插入到 div 中),它会在加载后立即执行。像这样:

<div>
    Insert the inner html content of that pages here and add this script at the bottom and add the onload function of the original html to this script.
    <script type="javascript">
       alert("hello world");
    </script>
</div>

Just remember to have the javascript available to your page. What I mean is that if the javascript function called which is called inside onload="..."is defined in the <head>of the loading html document and you're throwing the <head>out then this won't work.

请记住让 javascript 可用于您的页面。我的意思是,如果在加载的 html 文档中onload="..."定义了在内部调用的 javascript 函数,<head>并且您将其抛出<head>,那么这将不起作用。

回答by JerryGoyal

Not the best but one way is to check the div periodically if it's loaded:

不是最好的,但一种方法是在加载 div 时定期检查它:

var myInterval = setInterval(function () {
if ($('.mydiv') && $('.mydiv').width() > 0) {
// write your logic here
clearInterval(myInterval);
    } 
}, 3000);

回答by Muhammad Bilal

If you want to add the onload event on a div, you cannot, but you can add onkeydown and trigger the onkeydown event on document load.

如果要在 div 上添加 onload 事件,则不能,但可以添加 onkeydown 并在文档加载时触发 onkeydown 事件。

<div onkeydown="setCss();"></div>

$(function () {
    $(".ccsdvCotentPS").trigger("onkeydown");
});