简体   繁体   中英

Returning HTML from a javascript file

I want to return HTML Code from a .js file.

Here is my code:

HTML:

...
..
.


<script id="message-response-template" type="text/x-handlebars-template">
    <div class="sender-message-line table-content vivify swoopInBottom">

        <div class="table-content-cell" id="ava">
            <img src="/static//img/pro_2.png" alt="DP" class="avatar">
        </div>

        <div class="msg-conatiner">
            <div class="actual-msg"></div>

            <span>Message</span>

            <div class="">
                <div class="message-seen text-right">{{time}}</div>
            </div>
        </div>
    </div>
</script>

...
..
.

JS

        setTimeout(function () {
            var contextResponse = {
                response:$('HTML CODE I WANT TO RETURN').html(),
                time: this.getCurrentTime()
            };

I can only return string files but when I want to return the whole HTML code that I wrote up there, nothing shows up

I tried :

response: $('<script id="message-response-template" type="text/x-handlebars-template"> <div class="sender-message-line table-content vivify swoopInBottom"> <div class="table-content-cell" id="ava"> <img src="/static//img/pro_2.png" alt="DP" class="avatar"> </div> <div class="msg-conatiner"> <div class="actual-msg"></div> <span>Message</span> <div class=""> <div class="message-seen text-right">{{time}}</div> </div> </div> </div> </script>').html()

If you're using Jquery:

$("#yourDiv").load('yourhtmlfile.html #readMe');

And add the id to your html:

<div><div id="readMe"><p>text</p></div></div>

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