如何根据if语句替换图像?

时间:2015-10-02 14:27:59

标签: javascript html

我编写了一个if语句,它将根据x的值为1或0选择一个图标.hlpTbl是一个弹出窗口,显示一些有用的链接,旁边有图标。根据他们是否遇到员工或经理,图标应更改为documentIcon.gif或mgrIcon.gif。

语句“imgpath + icon”是我遇到错误的地方。有什么建议?

        function setHelpContent(ctntType){
            var numSects = mycontent.helpTitleArrSz;
            var numItems = 0;
            var hlpTbl = "<div class=\"closeBtn\" height=\"25px\"><a href=\"javascript: hidePopup();\" class=\"whtNavLinks\">[x]</a></div><table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" id=\"helpTbl\"class=\"helpTbl\">";

            for (x=0; x<=numSects; x++){
                hlpTbl = hlpTbl + "<tr id='helpTitle" + x + "'><td class='helpTitle'>" + mycontent["helpTitle_" + x] + "</td></tr>";
                hlpTbl = hlpTbl + "<tr><td><div align=\"center\" id=\"helpCtg_" + x +"\" class=\"hlpCtgDiv\"><table align=\"center\" class=\"hlpCtgTbl\">";

                if ( x == 0 ){
                    var icon = "documentIcon.gif\"
                    return icon;
                }
                else if ( x == 1 ){
                    var icon = "mgrIcon.gif\"
                    return icon;
                }

                for (i=0; i<=mycontent["helpContent" + x + "_Sz"]; i++){
                    hlpTbl = hlpTbl + '<tr class="helpItem" id="helpItemRow' + x + i + '" onmouseover="javascript: document.getElementById(\'helpItemRow' + x + i + '\').className = \'helpItemHiglight\';" onmouseout="javascript: document.getElementById(\'helpItemRow' + x + i + '\').className = \'helpItem\';" onclick="javascript: openWin(\'' + mycontent["helpContent_" + x + "_" + i + "_link"] + '\')"><td class="helpborder">';
                    hlpTbl = hlpTbl + "<img src=\"" + imgpath + icon class=\"docIconSmall\"/></a></td><td style=\"border-bottom: solid 1px; border-color: #8E88F7;\">";
                    hlpTbl = hlpTbl + "<div style=\"position: relative; left: 7px; width: 440px; text-align: left;\">" + mycontent["helpContent_" + x + "_" + i] + '</div></td></tr>';
                }
                hlpTbl = hlpTbl + "</div></table></td></tr>"
            }
            hlpTbl = hlpTbl + "</table><div>&nbsp;</div>"
            document.getElementById("popupTbl").innerHTML = hlpTbl;
            showPopup(contactsTbl_Class);
            expndBlock('helpCtg_' + ctntType,ctntType,numSects);
        }

1 个答案:

答案 0 :(得分:0)

我认为你有一个缺失的+号:

hlpTbl = hlpTbl + "<img src=\"" + imgpath + icon class=\"docIconSmall\"/></a></td><td style=\"border-bottom: solid 1px; border-color: #8E88F7;\">";

图标和类之间。应该是:

hlpTbl = hlpTbl + "<img src=\"" + imgpath + icon + " \"" + class=\"docIconSmall\"/></a></td><td style=\"border-bottom: solid 1px; border-color: #8E88F7;\">";