在 Javascript HTML 中添加 If / Else If 语句
Adding If / Else If Statment Inside Javascript HTML
好的,我有一个服务器端数据表读取 JSON 格式的子行。
我怎样才能在两者之间添加 If / Else If / Switch 语句?
<script type="text/javascript">
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',
'<tbody>'+
'<tr>'+
'<td class="second-td-box">'+
'<p class="at-a-glance">Basic information:</p>'+
'<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
</script>
我试过:
<script type="text/javascript">
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',
'<tbody>'+
'<tr>'+
'<td class="second-td-box">'+ ''
if (d.RES_GUEST_FIRSTNAME == "Alex") {
'<p class="at-a-glance">Basic information:</p>'+
'<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+
}
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
</script>
使用以下代码解决了问题:
((d.BOOKING_SOURCE_ID == 73844)?
'<td class="second-td-box">'+
'<p class="at-a-glance">Airbnb information:</p>'+
'<p><span class="subhead">Confirmation: </span>'+d.CONFIRMATION+'</p>'+
'<p><span class="subhead">Agency Income: </span>'+d.AIRBNB_AGENCY_INCOME+'</p>'+
'<p><span class="subhead">Airbnb Income: </span>'+d.AIRBNB_INCOME+'</p>'+
'<p><span class="subhead">Airbnb fees: </span>'+d.AIRBNB_FEES+'</p>'+
'<p><span class="subhead">Airbnb per night: </span>'+d.AIRBNB_PER_NIGHT+'</p>'+
'<span id="chart_div"></span>'
:"")+
'</td>'+
((d.BOOKING_SOURCE_ID == 73858)?
'<td class="second-td-box">'+
'<p class="at-a-glance">Booking.com information:</p>'+
'<p><span class="subhead">Booking date: </span>'+d.BOOKING_DATE+'</p>'+
'<p><span class="subhead">Booking currency: </span>'+d.BOOKING_CURRENCY+'</p>'+
'<p><span class="subhead">Booking total: </span>'+d.BOOKING_TOTAL+'</p>'+
'<p><span class="subhead">Booking comission: </span>'+d.BOOKING_COMISSION+'</p>'+
'<p><span class="subhead">Booking policy: </span>'+d.BOOKING_POLICY+'</p>'+
'<span id="chart_div"></span>'
:"")+
'</td>'+
'</tr>'+
看来我只能"Print"使用条件运算符。
您可以尝试条件运算符 (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Conditional_Operator):
<script type="text/javascript">
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',
'<tbody>'+
'<tr>'+
'<td class="second-td-box">'+ '' +
((d.RES_GUEST_FIRSTNAME == "Alex")?
'<p class="at-a-glance">Basic information:</p>'+
'<p><span class="subhead">Name:</span>'+d.RES_GUEST_FIRSTNAME+'</p>'
:"")+
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
</script>
连接 with 时不能使用 if 语句。你可以使用 conditional operator 这样的东西:
'string' + d.RES_GUEST_FIRSTNAME ? 'add string' : '' + 'another string';
return 'string ='+(d.RES_GUEST_FIRSTNAME == "Alex")?'yes':'no';
用法:
(condition)? [value if true] : [value if false];
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
本质上你是在 returning 一个字符串,所以你可以一点一点地建立它。我要强调的是 returning HTML as string from JavaScript 不是推荐的注入 HTML.
的方法
var returnString = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
returnString += '<tbody>';
returnString += '<tr>';
等等..
然后return字符串
return returnString;
因此,如果您想添加一个 if/if else
然后你可以做类似的事情
if (d.RES_GUEST_FIRSTNAME == "Alex") {
returnString += '<p class="at-a-glance">Basic information:</p>';
}
如果您正在使用 ``
,这可能对您有所帮助。
`<div class="user-email">
${response[i].email ? '<i class="fas fa-envelope"></i>': ""}
${response[i].email}
</div>
<div class="user-phone">
${response[i].phone ? '<i class="fas fa-phone-alt"></i>': ""}
${response[i].phone}
</div>`
好的,我有一个服务器端数据表读取 JSON 格式的子行。 我怎样才能在两者之间添加 If / Else If / Switch 语句?
<script type="text/javascript">
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',
'<tbody>'+
'<tr>'+
'<td class="second-td-box">'+
'<p class="at-a-glance">Basic information:</p>'+
'<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
</script>
我试过:
<script type="text/javascript">
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',
'<tbody>'+
'<tr>'+
'<td class="second-td-box">'+ ''
if (d.RES_GUEST_FIRSTNAME == "Alex") {
'<p class="at-a-glance">Basic information:</p>'+
'<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+
}
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
</script>
使用以下代码解决了问题:
((d.BOOKING_SOURCE_ID == 73844)?
'<td class="second-td-box">'+
'<p class="at-a-glance">Airbnb information:</p>'+
'<p><span class="subhead">Confirmation: </span>'+d.CONFIRMATION+'</p>'+
'<p><span class="subhead">Agency Income: </span>'+d.AIRBNB_AGENCY_INCOME+'</p>'+
'<p><span class="subhead">Airbnb Income: </span>'+d.AIRBNB_INCOME+'</p>'+
'<p><span class="subhead">Airbnb fees: </span>'+d.AIRBNB_FEES+'</p>'+
'<p><span class="subhead">Airbnb per night: </span>'+d.AIRBNB_PER_NIGHT+'</p>'+
'<span id="chart_div"></span>'
:"")+
'</td>'+
((d.BOOKING_SOURCE_ID == 73858)?
'<td class="second-td-box">'+
'<p class="at-a-glance">Booking.com information:</p>'+
'<p><span class="subhead">Booking date: </span>'+d.BOOKING_DATE+'</p>'+
'<p><span class="subhead">Booking currency: </span>'+d.BOOKING_CURRENCY+'</p>'+
'<p><span class="subhead">Booking total: </span>'+d.BOOKING_TOTAL+'</p>'+
'<p><span class="subhead">Booking comission: </span>'+d.BOOKING_COMISSION+'</p>'+
'<p><span class="subhead">Booking policy: </span>'+d.BOOKING_POLICY+'</p>'+
'<span id="chart_div"></span>'
:"")+
'</td>'+
'</tr>'+
看来我只能"Print"使用条件运算符。
您可以尝试条件运算符 (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Conditional_Operator):
<script type="text/javascript">
function format ( d ) {
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">',
'<tbody>'+
'<tr>'+
'<td class="second-td-box">'+ '' +
((d.RES_GUEST_FIRSTNAME == "Alex")?
'<p class="at-a-glance">Basic information:</p>'+
'<p><span class="subhead">Name:</span>'+d.RES_GUEST_FIRSTNAME+'</p>'
:"")+
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
</script>
连接 with 时不能使用 if 语句。你可以使用 conditional operator 这样的东西:
'string' + d.RES_GUEST_FIRSTNAME ? 'add string' : '' + 'another string';
return 'string ='+(d.RES_GUEST_FIRSTNAME == "Alex")?'yes':'no';
用法:
(condition)? [value if true] : [value if false];
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
本质上你是在 returning 一个字符串,所以你可以一点一点地建立它。我要强调的是 returning HTML as string from JavaScript 不是推荐的注入 HTML.
的方法var returnString = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
returnString += '<tbody>';
returnString += '<tr>';
等等..
然后return字符串
return returnString;
因此,如果您想添加一个 if/if else
然后你可以做类似的事情
if (d.RES_GUEST_FIRSTNAME == "Alex") {
returnString += '<p class="at-a-glance">Basic information:</p>';
}
如果您正在使用 ``
,这可能对您有所帮助。
`<div class="user-email">
${response[i].email ? '<i class="fas fa-envelope"></i>': ""}
${response[i].email}
</div>
<div class="user-phone">
${response[i].phone ? '<i class="fas fa-phone-alt"></i>': ""}
${response[i].phone}
</div>`