简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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