使用jquery将javascript添加到html页面中 [英] add javascript into a html page with jquery

查看:85
本文介绍了使用jquery将javascript添加到html页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个javascript google广告,但是我无法使用jquery将javascript插入到div中。我试图用这个测试来模拟我的问题,它使用了我在stackoverflow上发现的一些建议,但它不起作用。

;在div中插入script type ='text / javascript'> document.write('hello world');< / script> ,并在标签__1之间显示hello world




$ b

;
< head>
< script type =text / javascriptsrc =jquery.js>< / script>
< script type =text / javascript>
$(document).ready(function(){
var str =< script type ='text / javascript'> document.write('hello world');;
str + =<;
str + =/ script>;

$('#insert_here')。append(str);
});
< / script>
< / head>
< body>
tag_1< br />
< div id =insert_here>
< / div>
tag_2< br />
< / body>
< / html>

坦克为您的答案,



Lucas

解决方案

我想出了一个很好的解决方案:


  1. 将Google Adsense代码插入网页的任意位置 - 例如如果您的CMS只允许您将其放在右侧,请将其粘贴在那里。 使用 display:none style

  2. 添加一些jquery代码将div移动到您想要的位置。

由于javascript已经运行,因此将脚本块移到无论你喜欢它在哪里。



例如如果你希望在你的博客中散布2块谷歌广告(比如在第1段和第4段之后),那么这是完美的。



以下是一些示例代码:

 < div id =advert1style =display:none> 
< div class =advertbox advertfont>
< div style =float:right;>
< script type =text / javascript><! -
google_ad_client =pub-xxxxxxxxxxxxxxxxx;
/ *视频框* /
google_ad_slot =xxxxxxxxxxxxxxxxx
google_ad_width = 300;
google_ad_height = 250;
// - >
< / script>
< script type =text / javascript
src =http://pagead2.googlesyndication.com/pagead/show_ads.js>
< / script>
< / div>
< / div>
< / div>

< script>
$(document).ready(function(){
$ b $('#advert1')。appendTo(#content p:eq(1));
$ ('#advert1').css(display,block);

});
< / script>

p.s。 #content恰好是内容在我的CMS(Squarespace)上启动的位置,因此您可以用CMS中的任何内容替换它。这是一种享受,并且不会破坏Google ToS。


I want to add a javascript google ad but I can't insert the javascript into the div using jquery. I try to simulate my problem with this test, which is using some advice I found on stackoverflow , but it does not work.

I want <script type='text/javascript'>document.write('hello world');</script> to be inserted in the div, and "hello world" be displayed between the tag_1 and tag_2.

Here is the code :

<html>
    <head>
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript">
         $(document).ready(function() {
         var str="<script type='text/javascript'>document.write('hello world');";
         str+="<";
         str+="/script>";

         $('#insert_here').append(str);
         });
      </script>
    </head>
    <body>
      tag_1<br/>
      <div id="insert_here">
      </div>
      tag_2<br/>
    </body>
</html>

Tanks for your answers,

Lucas

解决方案

I figured out a great solution:

  1. Insert your Google Adsense code anywhere on your page - e.g. if your CMS only allows you to put this on the right hand side then stick it there.
  2. Wrap a div around it with display:none style
  3. Add some jquery code to move the div to the location you desire.

Since the javascript has already run there is no problem then with moving the block of script to wherever you'd like it to be.

e.g. if you wish to put 2 blocks of google adverts interspersed throughout your blog (say after paragraph 1 and after paragraph 4) then this is perfect.

Here's some example code:

<div id="advert1" style="display:none">
<div class="advertbox advertfont">
    <div style="float:right;">
        <script type="text/javascript"><!--
        google_ad_client = "pub-xxxxxxxxxxxxxxxxx";
        /* Video box */
        google_ad_slot = "xxxxxxxxxxxxxxxxx"
        google_ad_width = 300;
        google_ad_height = 250;
        //-->
        </script>
        <script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
        </script>
    </div>
</div>
</div>

<script>
$(document).ready(function() {

$('#advert1').appendTo("#content p:eq(1)");
$('#advert1').css("display", "block");

});
</script>

p.s. #content happens to be where the content starts on my CMS (Squarespace) so you can replace that with whatever you have in your CMS. This works a treat and doesn't break Google ToS.

这篇关于使用jquery将javascript添加到html页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆