2011-05-02 45 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