Py学习  »  Jquery

用jquery查找div类号的问题

batuhan • 5 年前 • 1295 次点击  

我想找到一些div类,并在类2之后添加一个代码。

更新

在编写另一个代码而不是google广告代码时,它可以正常工作。

 <script>
  $(function()
 {
var say = $('.panel-footer.clearfix').length;
if(say >= 3){
     $('.panel-footer.clearfix')[1].after('<script async 
 src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
 style="display:inline-block;width:320px;height:100px"
 data-ad-client="ca-pub-3767785488130692"
 data-ad-slot="3213073317"></ins>
 <script>
  (adsbygoogle = window.adsbygoogle || []).push({});
  </script>');
}
   });  

但没用。代码运行在页面的底部,而不是第二个div。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44185
 
1295 次点击  
文章 [ 3 ]  |  最新文章 5 年前
A-312
Reply   •   1 楼
A-312    6 年前
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="panel-footer clearfix"></div>
<div class="panel-footer clearfix"></div>
<div class="panel-footer clearfix"></div>
<script>
function insert_adsbygoogle(arg1) {
  //https://support.google.com/adsense/answer/7477845?hl=en
  (window.adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-3767785488130692",
    data_ad_slot: "3213073317",
    enable_page_level_ads: true
  });
  var say = $('.panel-footer.clearfix').length;
  if (say >= 3) {
    $('.panel-footer.clearfix').eq(1).after("<div id=write_container_for_adsbygoogle></div>");

    // container is where you want the ad to be inserted

    var script = document.createElement("script");
    script.type = "text/javascript";
    script.setAttribute("async", "");
    script.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
    $('#write_container_for_adsbygoogle').after(script);
  } else if (arg1 === "first") {
    $(window).on("load", insert_adsbygoogle);
  }
}
insert_adsbygoogle("first");
</script>
Diego Rosales
Reply   •   2 楼
Diego Rosales    6 年前

您可以通过使用jquery返回的数组的索引来实现这一点,而且length不是一个函数,所以它只是 length .

$(function()
{
    var say = $('.panel-footer clearfix').length;
    if(say >= 3){
       $('.panel-footer clearfix')[1].after('<?php echo get_option("my_php_code");?>');
    }
 });

如果clearfix是一个类,则必须使用: $('.panel-footer.clearfix') 如另一评论所述。

Abid
Reply   •   3 楼
Abid    6 年前

假设clearfix是类名

 <script>
         $(function()
     {
        var say = $('.panel-footer.clearfix').length();
         if(say >= 3){
         $('.panel-footer.clearfix:eq(2)').after('<?php echo 
        get_option("my_php_code");?>');
     }
      });
     </script>