2015-12-21 87 views
-1

我的if語句不工作。有人可以告訴我如何檢查我的a.style.background是否等於我的網址open.png?我有下一個代碼。如果聲明JavaScript不起作用

HTML

<a id="mnav" href="#" onclick="nav()" onmouseover="change()" onmouseout="back()"></a> 

的JavaScript

var a = document.getElementById('mnav') 
    if(a.style.backgroundImage == "url(open.png)"){ 
      alert("help"); 
      a.style.backgroundImage="url(navopen.png)"; 
    } 

對不起我的英語不好:)使用

https://jsfiddle.net/z8tz02hg/

+4

什麼,當你運行'的console.log(a.style.backgroundImage)發生;' – taylorc93

+1

顯示了'標籤'的HTML –

+0

嘗試在將backgroundImage使用toLowerCase()......此外,還要確保您的圖片位於您參考的文件夾中。 –

回答

0

嘗試:

if(a.style.backgroundImage.indexOf('url("open.png")') >= 0){ 
 
    alert("help"); 
 
    a.style.backgroundImage='url("navopen.png")'; 
 
}