日期:2014-05-17 浏览次数:20581 次
<html> <head> <script type="text/javascript" src="http://localhost/mycenter/js/jquery-1.4.min.js"></script> <script type="text/javascript"> // 单选 function single_click(){ $("body *").click(function(e){ $(".selected").removeClass("selected"); $(e.target).addClass("selected"); }); } // 多选 function shift_click(){ // 这里怎么写?只要按shift 再左键单击就能多选一个.selected } $(function(){ single_click(); shift_click(); }); </script> <style type="text/css"> .selected{background:green;} </style> </head> <body> <p>I am p</p> <span>I am span</span> <div> I am div</div> </body> </html>
------解决方案--------------------
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <style type="text/css"> tr.selected { background: blue; color: white} /* 选中的行用蓝底白字显示 */ </style> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <!-- 隐藏域存放选中的行号,用逗号隔开 --> <asp:HiddenField ID="SelectedRows" runat="server" EnableViewState="false" /> <script type="text/javascript"> $(function() { var $gv = $('#GridView1'), $rows = $('#SelectedRows'); // GridView和SelectedRows控件 var headerRows = <%= GridView1.ShowHeader? 1 : 0 %>; // 标题行数 $gv.find('tr').click(function(evt) { $gv.find('tr').removeClass('selected'); if (!evt.shiftKey || !$rows.val()) { $(this).addClass('selected'); $rows.val(this.rowIndex - headerRows); } else { var first = Number($rows.val().split(',')[0])+headerRows, last = this.rowIndex, rows = []; for (var i = 0, index = first; i <= Math.abs(first - last); i++) { $gv.find('tr:eq(' + index + ')').addClass('selected');