简体   繁体   English

javascript html插入带if语句的变量

[英]javascript html into variable with if statements

I am getting values from array and I am using those values in html block and storing that black into variable but I am getting an error "unexpected token if" 我从数组中获取值,并且在html块中使用这些值并将该黑色存储到变量中,但是出现错误“如果出现意外的标记,”

My code look like this. 我的代码如下所示。

@: marker.contentString = '<div class="content" style="width:400px;height:400px;">' +
@:  '<h3><b>'+ markerpoints[i].coursename +'</b></h3>' +
@:  '<p>'+ markerpoints[i].country  +'</p>'+
@:  '<table id="map_table" style="margin-top:5px;" width="100%" cellpadding="10" cellspacing="5">'+
@:  '<thead style="text-align:left"><tr><th>Date</th><th>Course</th></tr></thead>'+
@:  '<tbody>'+
@:  if(markerpoints[i].ProductName0 != 'undefined'){
@:  '<tr>'+
@:  '<td width="158">'+ markerpoints[i].Fromdate0 +' - '+ markerpoints[i].Todate0 +'</td>'+
@:  '<td><a href="Default.aspx?ID=771&ProductID='+ markerpoints[i].ProductID0 +'">'+ markerpoints[i].ProductName0 +'</a></td>'+
@:  '</tr>'+
@:          }
@:  if(markerpoints[i].ProductName1 !== 'undefined'){
@:  '<tr>'+
@:  '<td width="158">'+ markerpoints[i].Fromdate1 +' - '+ markerpoints[i].Todate1 +'</td>'+
@:  '<td><a href="Default.aspx?ID=771&ProductID='+ markerpoints[i].ProductID1 +'">'+ markerpoints[i].ProductName1 +'</a></td>'+
@:  '</tr>'+
@:      }
@:  if(markerpoints[i].ProductName2 !== 'undefined'){
@:  '<tr>'+
@: '<td width="158">'+ markerpoints[i].Fromdate2 +' - '+ markerpoints[i].Todate2 +'</td>'+
@:  '<td><a href="Default.aspx?ID=771&ProductID='+ markerpoints[i].ProductID2 +'">'+ markerpoints[i].ProductName2 +'</a></td>'+
@:  '</tr>'+
@:                                              }
@:  '</tbody>'+
@:  '</table>'+
@:  '</div>'

is this a correct way of writing if statement in this scenario if not how i can prevent that is value is undefined it should not out put value as undefined. 这是在这种情况下编写if语句的正确方法吗?

You need a ";" 你需要一个 ”;” before the if, then you need to resume the string concatenation with marker.contentString += '<.... 在if之前,那么您需要使用marker.contentString + ='<....恢复字符串连接。

@: marker.contentString = '<div class="content" style="width:400px;height:400px;">' +
    @:  '<h3><b>'+ markerpoints[i].coursename +'</b></h3>' +
    @:  '<p>'+ markerpoints[i].country  +'</p>'+
    @:  '<table id="map_table" style="margin-top:5px;" width="100%" cellpadding="10" cellspacing="5">'+
    @:  '<thead style="text-align:left"><tr><th>Date</th><th>Course</th></tr></thead>'+
    @:  '<tbody>';
    @:  if(markerpoints[i].ProductName0 != 'undefined'){
marker.contentString += 'etc....

try this 尝试这个

  marker.contentString = '<div class="content" style="width:400px;height:400px;">' + '<h3><b>'+ markerpoints[i].coursename +'</b></h3>' + '<p>'+ markerpoints[i].country +'</p>'+ '<table id="map_table" style="margin-top:5px;" width="100%" cellpadding="10" cellspacing="5">'+ '<thead style="text-align:left"><tr><th>Date</th><th>Course</th></tr></thead>'+ '<tbody>'; if(markerpoints[i].ProductName0 != 'undefined'){ marker.contentString = marker.contentString +'<tr>'+ '<td width="158">'+ markerpoints[i].Fromdate0 +' - '+ markerpoints[i].Todate0 +'</td>'+ '<td><a href="Default.aspx?ID=771&ProductID='+ markerpoints[i].ProductID0 +'">'+ markerpoints[i].ProductName0 +'</a></td>'+ '</tr>'; } if(markerpoints[i].ProductName1 !== 'undefined'){ marker.contentString = marker.contentString +'<tr>'+ '<td width="158">'+ markerpoints[i].Fromdate1 +' - '+ markerpoints[i].Todate1 +'</td>'+ '<td><a href="Default.aspx?ID=771&ProductID='+ markerpoints[i].ProductID1 +'">'+ markerpoints[i].ProductName1 +'</a></td>'+ '</tr>'; } if(markerpoints[i].ProductName2 !== 'undefined'){ marker.contentString = marker.contentString +'<tr>'+ '<td width="158">'+ markerpoints[i].Fromdate2 +' - '+ markerpoints[i].Todate2 +'</td>'+ '<td><a href="Default.aspx?ID=771&ProductID='+ markerpoints[i].ProductID2 +'">'+ markerpoints[i].ProductName2 +'</a></td>'+ '</tr>'; } marker.contentString = marker.contentString +'</tbody>'+ '</table>'+ '</div>'; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM