简体   繁体   English

使用Jquery在div之后插入/加载javascript文件

[英]Inserting/loading a javascript file after a div using Jquery

I wanted to insert this ad code. 我想插入此广告代码。

 <script type="text/javascript" src="http://website.com/wp-content/plugins/oiopub-direct/js.php?type=banner&align=center&zone=1"></script>

right after 之后

<div id="adminmenu"></div>

So that the output of the script will be displayed after the adminmenu div 这样脚本的输出就会显示在adminmenu div之后

Is there a way to do that using jquery? 有没有办法使用jquery做到这一点? I tried .insertAfter but it only works on plain html. 我试过.insertAfter但它只适用于普通的html。

Render the content first in a hidden, temporary container: 首先在隐藏的临时容器中呈现内容:

<div class="temp">
    <script type="text/javascript" src="http://website.com/wp-content/plugins/oiopub-direct/js.php?type=banner&align=center&zone=1"></script>
</div>

then move the contents after it is rendered: 然后在渲染后移动内容:

$('.temp').contents().appendTo(finalDestination) 

If a script is using document.write() to emit content, then you have to place the script in the place that you want the content to go. 如果脚本使用document.write()发出内容,则必须将脚本放在您希望内容发布的位置。

If you control the script, then you can have the script not emit it's content by default and you can call a function in the script and pass a parent object that directs the script where it should put it's content (which it will not use document.write() to create). 如果您控制脚本,则默认情况下您可以让脚本不发出它的内容,您可以在脚本中调用一个函数并传递一个父对象,该对象将脚本指向应该放置它的内容(它不会使用document.write()创建)。

The only other option is to place a div around the script, let it emit the content into that div and then move that container div to another location in the page after it runs. 唯一的另一个选择是在脚本周围放置一个div,让它将内容发送到该div中,然后在运行后将该容器div移动到页面中的另一个位置。

<div id="ads">
<script type="text/javascript" src="http://website.com/wp-content/plugins/oiopub-direct/js.php?type=banner&align=center&zone=1"></script>
</div>
<script>
$(document).ready(function() {
    $("#ads").appendTo("#myFooter");
});
</script>

I'm not sure exactly what you're trying to ask about jQuery. 我不确定你要问的关于jQuery的确切内容。 jQuery is capable of inserting dynamically created content anywhere you want in the page. jQuery能够在页面中的任何位置插入动态创建的内容。 You'd have to ask a more specific question for us to advise in more details. 您需要向我们提出更具体的问题,以便提供更多详细信息。

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

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