簡體   English   中英

setinterval無法更改圖像src

[英]setinterval not working to change image src

想知道為什么以下代碼不會更改我的img src。 注釋掉的示例效果很好。 警報每4秒觸發一次。 替換src只會在set interval函數內部起作用。

$(document).ready(function(){
    $(".samples_1_1").on("click", function(){
        alert('asdf');
        //$(this).attr("src", "../../static/results/samples_1_2.png");
        setInterval(function() {
          alert('alert 1');
          $(this).src("src", "../../static/results/samples_1_2.png");        
        }, 4000);
      });
    });

首先,在第二個示例中,您使用的.src()不存在。 像以前一樣使用.attr()。 其次, $(this)關鍵字現在位於其自己的函數內,從而使其未定義。 您需要使用$(".samples_1_1").attr("src", "../../static/results/samples_1_2.png");手動指定元素$(".samples_1_1").attr("src", "../../static/results/samples_1_2.png");

暫無
暫無

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

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