我从数组中获取值,并且在 html block 中使用这些值并将黑色存储到变量中,但我收到错误“unexpected token if”
我的代码如下所示。
@: 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>'
在这种情况下,这是编写 if 语句的正确方法吗?如果不是我可以防止该值未定义,则不应将值输出为未定义。
最佳答案
你需要一个“;”在 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....
关于javascript html 通过 if 语句进入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31204800/