簡體   English   中英

jQuery在控制台上工作,但不在頁面上

[英]Jquery working on console but not on page

我試圖在某個時間后將div加載並在控制台中工作時隱藏div,但是當我在實際網頁中放置此代碼時,它不起作用。

 (function ($) {
  if ($('#insightera_widget_content').length) {
  $('#insightera_widget_content').delay(10000).fadeOut('slow');
  }
}(jQuery));

當我也查看源代碼時,也看不到div Insightera_widget_content ,但是我可以在Inspector上看到它。 它從某些外部小部件加載。

在此處輸入圖片說明 我也嘗試在頁面上使用以下內容,但它根本不起作用。

  <script>
    $(document).ready(function() { 
    if ($('#insightera_widget_content').length) { 
    $('#insightera_widget_content').delay(10000).fadeOut('slow');}
    }); 
    </script>

有什么建議么?

設置一個setInterval()來檢查元素的存在,一旦找到它,就運行您的jquery並清除間隔。

 var interval = setInterval(function() { if ($('#insightera_widget_content').length) { $('#insightera_widget_content').delay(10000).fadeOut('slow'); clearInterval(interval); } }, 1000) /* you don't need this part - just simulating the widget adding the code to the page */ setTimeout(function() { $('body').append('<div id="insightera_widget_content">insightera_widget_content</div>'); }, 5000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM