Javascript更改图标

时间:2015-04-09 20:28:00

标签: javascript jquery-mobile icons

我尝试使用select元素更改图标。我用2个值制作了它,但现在我需要3个。

知道这段代码有什么问题吗?

var icon = document.getElementById.("marker-icon"); 
    if (type == 1) {  
        marker-icon.src = "images/icon1.png";
    } else if (type == 2) {
        marker-icon.src = "images/icon2.png";
    } else if (type == 3) {
        marker-icon.src = "images/icon3.png";
}

此代码适用于2个值,并且工作正常。

var icon = (type == 1) ? "images/icon1.png" : "images/icon2.png";

3 个答案:

答案 0 :(得分:0)

尝试使用switch case语法。

switch (type) {
    case 1:
        var icon =  "images/icon1.png";
        break;
    case 2:
        var icon =  "images/icon2.png";
        break;
    case 3:
        var icon =  "images/icon3.png";
        break;
    default:
        //default code block
        break;
}

答案 1 :(得分:0)

试试这个:

var icon = document.getElementById("marker-icon"); 
if (type == 1) {  
    icon.src = "images/icon1.png";
} else if (type == 2) {
    icon.src = "images/icon2.png";
} else if (type == 3) {
    icon.src = "images/icon3.png";
}

.之后有一个额外的getElementById,您使用的是marker-icon而不是icon。 (我假设marker-iconid代码的img。)

答案 2 :(得分:0)

适用于:

var icon = document.getElementById("icon"); 
    if (type == 1) {  
        icon = "images/icon1.png";
    } else if (type == 2) {
        icon = "images/icon2.png";
    } else if (type == 3) {
        icon = "images/icon3.png";
    }

谢谢大家! ^^