Jquery之table中根據行選中,進行背景變色和radio選中

來源:互聯網
上載者:User

Jquery之table中根據行選中,進行背景變色和radio選中
實現功能:點擊列表中的某一行,然後當前行會變成其他顏色,並且其中的radio會被選中。 

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><script type="text/javascript" src=\'#\'" /js/jquery-1.4.2.min.js"></script><script type="text/javascript">$(document).ready(function(){    $("tbody>tr").click(function(){        $(this)            .addClass('sel')            .siblings().removeClass('sel');            .end()            .find(':radio').attr('checked',true);    });})</script><style type="text/css">.sel{  background: red;}</style></head><body>    <table>    <thead>         <tr><th>選擇</th></tr>         <tr><th>姓名</th></tr>    <thead>    <tbody>       <tr><td><input type="radio" name="fruit"></td><td>張三</td></tr>       <tr><td><input type="radio" name="fruit"></td><td>李四</td></tr>       <tr><td><input type="radio" name="fruit"></td><td>王五</td></tr>    </tbody>  </table></body></html>

 

 學習之初對於end的用法不太瞭解,我把自己理解說下,歡迎指教。END()用法在官方文檔的說明回到最近的一個"破壞性"操作之前。即將匹配的元素列表變為前一次的狀態。相信不少人並沒有理解它的用法。舉個簡單例子 <p><span>Hello</span>,how are you?</p>jQuery 代碼:$("p").find("span").end()其返回的值為:[ <p><span>Hello</span> how are you?</p> ]其實就是:尋找P標籤中的<span>標籤,接著使用END()方法結束對P標籤的引用,此時返回的是P標籤(JQuery)對象就如第一個例子如果我採用end()用法,那麼寫法就是 
<script type="text/javascript">$(document).ready(function(){    $("tbody>tr").click(function(){        $(this)            .addClass('sel')            .siblings().removeClass('sel');        $(this).find(':radio').attr('checked',true);    });})</script>

 

 如果初始化的時候有表格被選中,則需要處理,代碼如下:$("table :radio:checked").parent().parent().addClass('sel');或者$("table :radio:checked").parents("tr").addClass('sel');或者$('tbody>tr:has(:checked)').addClass('sel');

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.