使用 JavaScript 将 HTML 加载到 DIV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9464737/
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-10-26 06:45:14  来源:igfitidea点击:

Using JavaScript to load HTML into DIV

javascripthtml

提问by Richard Eng

I'm trying to load some HTML into a DIV using JavaScript, but I can't alter the size of the DIV frame, no matter what I try. All I get is a small little frame in the middle of the browser window. I've tried CSS. I've tried 'width:' and 'height:'. Here's my code:

我正在尝试使用 JavaScript 将一些 HTML 加载到 DIV 中,但无论如何我都无法更改 DIV 框架的大小。我得到的只是浏览器窗口中间的一个小框架。我试过 CSS。我试过“宽度:”和“高度:”。这是我的代码:

<div id="main">
</div>
<script type="text/javascript">
    document.getElementById('main').innerHTML =
        '<object type="type/html" data="welcome.html"><\/object>';
</script>

Don't concern yourself about whyI want to do this -- it's part of a greater coding experiment.

不要担心我为什么要这样做——这是更大的编码实验的一部分。

回答by pedromarce

Can you use jquery? Then it becomes trivial...

你可以使用jquery吗?然后就变得微不足道了……

$('#main').load('welcome.html');

回答by Manatok

It seems to set the width

好像设置了宽度

document.getElementById('main').innerHTML ='<object type="type/html" data="newhtml.html"><\/object>';
document.getElementById('main').style.width = "600px";
document.getElementById('main').style.border = "solid red 1px";

shows that the width is set to 600px

显示宽度设置为 600px