我们可以在Javascript中的/ jQuery的 “innerHTML的”
问题描述:
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)
答
如果你坚持一个长字符串连接,你必须使用Ternary Operator (?:):
"some_long_string" +
(customerInfo == "changeCustomer" ?
'<p class="custTxtUnknown">YoYo</p>...' :
'<p class="custTxtUnknown">NO NO </p>...') +
"another_long_string"
答
是的,你可以。但是在这里你使用赋值运算符的条件。 “=”是赋值运算符。用于比较使用“==”。因此次的条件将现在
if(customerInfo=="changeCustomer")
答
如何:
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)