简体   繁体   中英

How to display a Javascript Tag with a src file inside a div

I need to display one javascript reference inside a div in the body content?

How do I insert a JS file inside the body part, not in the head , dynamically after the page loads?

HTML looks like this after the page loads:

<div class="cls1" id="id1">
    <script src="url" ></script>
</div>

I was trying,

$("#id1").append("<script src="www.example.com/ex.js" ></script>");

or

document.write("<script src="www.example.com/ex.js" ></script>");

.text将安全地添加它(转义html),而.append.html实际上会修改dom并执行您的javascript文件

$("#id1").text('<script src="www.example.com/ex.js" ></script>');

You use double inverted commas in append and also use double inverted commas for src 'attribute'. You have to use single inverted commas for src 'attribute'.

Try this code:

$("#id1").append("<script src='www.example.com/ex.js' ></script>");

or

document.write("<script src='www.example.com/ex.js'></script>");

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