2015-12-21 103 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")'; 
 
}