2011-05-02 129 views
1

这让我发疯 - 基本上这个如果语句不起作用。该代码不断跳入else语句,我已经悬停在buttonText上,并且一切看起来都是alrite,直到碰到条件。javascript如果语句没有工作

function DesignCodeViewClick(el) { 

    $("div.divdesigncodeviewbuttonsselected").attr("class", "divdesigncodeviewbuttons"); 
    $(el).attr("class", "divdesigncodeviewbuttonsselected"); 

    var buttonText = el.innerText; 

    if (buttonText.toLowerCase() == "design") { 
     $("#iframecms").css("display", "none"); 
     $("textarea.divhtmleditor").css("display", "none"); 
    } 
    else if (buttonText.toLowerCase() == "browse") { 
     $("#iframecms").css("display", "block"); 
     $("textarea.divhtmleditor").css("display", "none"); 
    } 
else { 

     $("textarea.divhtmleditor").css("display", "block"); 
     $("#iframecms").css("display", "none"); 
     WebForm_DoCallback('SEOCMSControl1', 'getcode~http://www.triksportfolio.com', GetCodeServerResponse, null, null, true); 

    } 

} 
+0

什么是HTML?可能是文本中有空白字符,例如新行或空格。 – 2011-05-02 11:34:57

+0

你调试过buttonText或el.innerText的值吗? – 2011-05-02 11:36:15

+3

您可能应该使用“.addClass()”和“.removeClass()”而不是“.attr()”来操作元素类。 – Pointy 2011-05-02 11:37:00

回答

4

textContent是标准属性。 innerText是我相信的Internet Explorer事情。它们几乎相同,但not quite

如果没有想这样做的,我建议这个...

var text; 
if ('textContent' in el) { 
    text = el.textContent; 
} else { 
    text = el.innerText; 
} 

,你使用jQuery,但是,因此就使用text()方法。

为确保没有前导或尾随空白,可以使用jQuery的trim()实用程序功能。

此外,您正在做一些jQuery可以帮助您的事情。看看addClass()hide()

+0

@alex它存在两种方法:.innerText和.innerHTML。 FF,Safari和Chrome中的示例支持这些标签。 'text()'使用与上面相同的方式。 – reporter 2011-05-02 11:38:53

+0

@reporter对不起,我不确定你的评论是关于什么的。 – alex 2011-05-02 11:39:29

+0

@alex innerText是原始的方法。女士发明了它,但所有的浏览器都支持这一点。但你是对的。对于干净的代码t操作应该使用jqery方法 – reporter 2011-05-02 11:42:17