2014-11-05 17 views
0

JS添加if/else語句:我們可以在Javascript中的/ jQuery的 「innerHTML的」

var overlayContainer = 
    '<div class="customerOverlayShadow">'+ 
     '<div class="customerOverlay">'+ 

      '<span class="customerCloseIcon" title="close"></span>'+ 
      '<div class="custWhtBg">'+ 
       '<h2 class="customerPopHeading">Government Account Validation</h2>'+ 
       '<div class="custGreytBg clearfix">'+ 


// here it starts 
        if(customerInfo="changeCustomer"){ 
         '<p class="custTxtUnknown">YoYo </p>'+ 
         '<p class="custTxtProcees">Would you like to proceed with using account 248578</p>'+ 
        } 
        else{ 
         '<p class="custTxtUnknown">NO NO </p>'+ 
         '<p class="custTxtProcees">Would you like to proceed with using account 248578</p>'+ 
        } 




       '</div>'+ 
      '</div>'+ 
     '</div>'+ 
    '</div>'; 
    console.log(overlayContainer) 

回答

1

如果你堅持一個長字符串連接,你必須使用Ternary Operator (?:)

"some_long_string" + 

(customerInfo == "changeCustomer" ? 
    '<p class="custTxtUnknown">YoYo</p>...' : 
    '<p class="custTxtUnknown">NO NO </p>...') + 

"another_long_string" 
0

是的,你可以。但是在這裏你使用賦值運算符的條件。 「=」是賦值運算符。用於比較使用「==」。因此次的條件將現在

if(customerInfo=="changeCustomer") 
2

如何:

var overlayContainer = 
     '<div class="customerOverlayShadow">'+ 
      '<div class="customerOverlay">'+ 

       '<span class="customerCloseIcon" title="close"></span>'+ 
       '<div class="custWhtBg">'+ 
        '<h2 class="customerPopHeading">Government Account Validation</h2>'+ 
        '<div class="custGreytBg clearfix">'; 
    // here it starts 
         if(customerInfo == "changeCustomer") { 
          overlayContainer += '<p class="custTxtUnknown">YoYo </p>'+ 
          '<p class="custTxtProcees">Would you like to proceed with using account 248578</p>'; 
         } 
         else { 
          overlayContainer += '<p class="custTxtUnknown">NO NO </p>'+ 
          '<p class="custTxtProcees">Would you like to proceed with using account 248578</p>'; 
         } 




    overlayContainer +='</div>'+ 
       '</div>'+ 
      '</div>'+ 
     '</div>'; 
     console.log(overlayContainer) 
相關問題