简体   繁体   English

更换 <div> 内容通过带有Javascript的外部html文件

[英]Replacing <div> content by external html file with Javascript

I'm a bit rusty on my HTML and JavaScript and can't seem to get this one right. 我对HTML和JavaScript有点生疏,似乎无法正确解决这一问题。 The simple approach I took works fine, but I want to be more flexible. 我采用的简单方法效果很好,但我想变得更加灵活。 Here is the simple approach: 这是简单的方法:

<head>

    <script>
        function loadContent(a){
            document.getElementById(a).innerHTML='<object type="text/html" data="home.html" ></object>';
        }
    </script>
</head>

<body>

    <div id="topBar"> 
        <a href ="#" onclick="loadContent('content')"> HOME </a> 
    </div>
    <div id ="content"> </div>

</body>

This works fine. 这很好。 However, I also want to get the desired document as a variable of the function. 但是,我也想将所需的文档作为函数的变量。 Here is where I get stuck. 这是我卡住的地方。 Probably has something to do with the "" and '', but I have not been able to find the answer. 可能与“”和“”有关,但我一直找不到答案。 Here is my 'desired' situation: 这是我的“期望”情况:

<head>

    <script>
        function loadContent(a, b){
            document.getElementById(a).innerHTML= b;
        }
    </script>
</head>

<body>

    <div id="topBar"> 
        <a href ="#" onclick="loadContent('content', '<object type='text/html' data='home.html' ></object>')"> HOME </a> 
    </div>
    <div id ="content"> </div>

</body>

Can anybody help me out? 有人可以帮我吗?

Thanx 感谢名单

Try: 尝试:

<a href ="#" onclick="loadContent('content', '<object type=\'text/html\' data=\'home.html\' ></object>')"> HOME </a> 

Basically just escape the quotes. 基本上只是escape报价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM