【问题标题】:UL>Li sorting not working in IE , working in chromeUL>Li 排序在 IE 中不起作用,在 chrome 中起作用
【发布时间】:2016-02-09 12:58:33
【问题描述】:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input type="button" id="sort" class="asc" value="asc" />

<ul class="list">
<li>Rahul</li>
<li>Vinit</li>
<li>Ajay</li>
<li>Zareena</li>
<li>Deepak</li>
</ul>
</body>
</html>

Jquery代码如下

$(document).ready(function() {  
    $('#sort').click(function(e) {    
       var $sort = this;    
      alert($($sort).html());
        var $list = $('.list');      
        var $listLi = $('li',$list);      
        $listLi.sort(function(a, b){
            var keyA = $(a).text();
            var keyB = $(b).text();         
          if($($sort).hasClass('asc')){
                return (keyA > keyB) ? 1 : 0;
            } else {
                return (keyA < keyB) ? 1 : 0;
            }
        });
        $.each($listLi, function(index, row){
            $list.append(row);
        });
      if($($sort).hasClass('asc'))
        {
          $($sort).removeClass('asc');      
          $($sort).prop('value', 'desc');

        }
      else
        {
          $($sort).addClass('asc');          
          $($sort).prop('value', 'asc');
        }
        e.preventDefault();
    });
});

我可以在 chrome 中完美运行此代码,但它无法在 IE(IE11) 中运行

JS BIN LINK

【问题讨论】:

    标签: jquery html google-chrome internet-explorer


    【解决方案1】:

    排序功能条件的修改完成了这项工作。我不确定为什么。

    DEMO

    $(document).ready(function() {  
        $('#sort').click(function(e) {    
           var $sort = this;    
            var $list = $('ul.list');      
            var $listLi = $list.children();      
            var btnclass = $($sort).hasClass('asc');
            $listLi.sort(function(a, b){
                var keyA = $(a).text().toUpperCase();
                var keyB = $(b).text().toUpperCase();
                if(btnclass)
                  return (keyA < keyB) ? -1 : (keyA > keyB) ? 1 : 0;
                else
                  return (keyA > keyB) ? -1 : (keyA < keyB) ? 1 : 0;
            });
            alert($listLi.map(function(){
              return $(this).text();
            }).get());
            $.each($listLi, function(index, row){
                $list.append(row);
            });
            if($($sort).hasClass('asc')){
                $($sort).removeClass('asc');      
                $($sort).prop('value', 'desc');
            } else {
                $($sort).addClass('asc');          
                $($sort).prop('value', 'asc');
            }
            e.preventDefault();
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 2015-03-31
      • 1970-01-01
      • 2020-06-04
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2012-01-23
      相关资源
      最近更新 更多