我们可以在Javascript' s / jQuery" innerhtml"中添加if / else语句

时间:2014-11-05 10:42:20

标签: javascript jquery

JS:

   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)

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

如果您坚持使用一个长字符串连接,则必须使用Ternary Operator (?:)

"some_long_string" +

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

"another_long_string"

答案 2 :(得分:0)

是的,你可以。但是这里你在条件中使用赋值运算符。 “=”是赋值运算符。比较使用“==”。所以这个条件现在是

if(customerInfo=="changeCustomer")