[英]Javascript changing source of an image
我有一些JavaScript,可運行一個計時器,對網站上的內容進行動畫處理。
一切都很好,但是我想讓圖像在動畫運行時發生變化,它使用jquery:
if(options.auto && dir=="next" && !clicked)
{
timeout = setTimeout(function(){
if (document.images['bullet1'].src == "img/bulletwhite.png")
{
document.images['bullet1'].src = "img/bullet.png";
document.images['bullet2'].src = "img/bulletwhite.png";
}
animate("next",false);
},diff*options.speed+options.pause);
}
options.auto表示其自動循環,dir是運動的方向,clicked是用戶是否單擊它。
我的語法有問題嗎? 我用它運行了螢火蟲,它沒有拋出任何錯誤,它根本行不通。 任何建議都會有所幫助!
我要提到的是bullet1的src從bulletwhite.png開始,然后我希望它更改為bullet.png並將bullet2更改為bulletwhite.png。
document.images['bullet1'].src == "img/bulletwhite.png"
您確定曾經滿足此條件嗎?
通常,imageElement.src 屬性包含src 屬性中內容的絕對/解析版本。 (這也適用於href屬性/屬性。)
<img src="/images/img1.png">
...
document.images[0].src => "http://127.0.0.1/images/img1.png"
根據您發布的代碼,第二行上有一個奇怪的分號,但也許只是錯字。
為什么不嘗試使用JQuery attr()調用而不使用document.images數組?
$('img[name="bullet1"]').attr('src', 'img/bullet.png');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.