if语句和for循环方法的错误附加如何解决?

时间:2017-10-14 07:59:16

标签: javascript jquery

错误在if语句的第一行,所以如果这个表达式为true,那么这行将被添加,否则另一行,如何修复它?

  参数列表

之后

SyntaxError:missing)

  $(".pagination_wrapper").append(
          '<ul class="new_pogination">' 
            if( data.previous_page != 0 ){
                + '<li>' + '<a href="?page=' + data.previous_page + '"' + 'onclick="sort_by_ajax()"' + 'class="prev_link"'+ '>' + '&laquo;' + '</a>' 
                + '</li>'
            }else{
                + '<li class="disabled">' + '<a class="disabled">' + '&laquo;' + '</a>' 
                + '</li>'
            };
            for ( i = 1; i < total_num; i++ ){
                if( i == current_page ){
                    + '<li class="active">' + '<a href="?page=' + i + '"' + 'onclick="sort_by_ajax()"' + '>' + i + '</a>' 
                    + '</li>'
                }else{
                    + '<li class="number_of_pagination">' + '<a href="?page=' + i + '"' + 'onclick="sort_by_ajax()"' + '>' + i + '</a>' 
                    + '</li>'
                };
            };

            if( data.next_page != total_num ){
                + '<li>' + '<a href="?page=' + data.next_page + '"' + 'onclick="sort_by_ajax()"' + 'class="next_link"' + '>' + '&raquo;' + '</a>' 
                + '</li>'
            }else{
                + '<li class="disabled">' + '<a class="disabled">' + '&raquo;' + '</a>' 
                + '</li>'
            };

            + '</ul>'
        );

2 个答案:

答案 0 :(得分:2)

试试这个!

&#13;
&#13;
$(".pagination_wrapper").append(function() {
      html = '<ul class="new_pogination">';
      
      if (data.previous_page != 0) {
        html += '<li>' + '<a href="?page=' + data.previous_page + '"' + 'onclick="sort_by_ajax()"' + 'class="prev_link"' + '>' + '&laquo;' + '</a>' + '</li>'
      } else {
        html += '<li class="disabled">' + '<a class="disabled">' + '&laquo;' + '</a>' + '</li>'
      };
      for (i = 1; i < total_num; i++) {
        if (i == current_page) {
          html += '<li class="active">' + '<a href="?page=' + i + '"' + 'onclick="sort_by_ajax()"' + '>' + i + '</a>' + '</li>'
        } else {
          html += '<li class="number_of_pagination">' + '<a href="?page=' + i + '"' + 'onclick="sort_by_ajax()"' + '>' + i + '</a>' +  '</li>'
        };
      };
    
      if (data.next_page != total_num) {
        html += '<li>' + '<a href="?page=' + data.next_page + '"' + 'onclick="sort_by_ajax()"' + 'class="next_link"' + '>' + '&raquo;' + '</a>' + '</li>'
      } else {
        html += '<li class="disabled">' + '<a class="disabled">' + '&raquo;' + '</a>' + '</li>'
      };
    
      html += '</ul>'
      return html;
    });
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这个错误通常意味着,你已经搞砸了语法。

在您的代码中,您使用函数变量中的嵌套+语句将if字符串连接成任何内容,其中只应使用逗号分隔元素...

更改您的方法,将您的逻辑放在需要字符串的函数参数中,创建字符串,然后将其传递给append()。例如:

// some assumptions
var total_num = 10;
var current_page = 4;
var data = {previous_page: 1, next_page: 11, current_page: 4}
// new approach

var result = '<ul class="new_pogination">';
if (data.previous_page != 0) {
    result += '<li><a href="?page=' + data.previous_page + '" onclick="sort_by_ajax()" class="prev_link">&laquo;</a></li>';
} else {
    result += '<li class="disabled"><a class="disabled">&laquo;</a></li>';
}
for (i = 1; i < total_num; i++) {
    if (i == current_page) {
        result += '<li class="active"><a href="?page=' + i + '" onclick="sort_by_ajax()">' + i + '</a></li>';
    } else {
        result += '<li class="number_of_pagination"><a href="?page=' + i + '" onclick="sort_by_ajax()">' + i + '</a></li>';
    };
}

if (data.next_page != total_num) {
    result += '<li><a href="?page=' + data.next_page + '" onclick="sort_by_ajax()" class="next_link">&raquo;</a></li>';
} else {
    result += '<li class="disabled"><a class="disabled">&raquo;</a></li>'
}


result += '</ul>';
 
    $(".pagination_wrapper").append(result);
.active{
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=pagination_wrapper></div>