Jquery之table中根据行选中,进行背景变色和radio选中

实现功能:点击列表中的某一行,然后当前行会变成其他颜色,并且其中的radio会被选中。

成都创新互联公司是专业的本溪网站建设公司,本溪接单;提供成都网站建设、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行本溪网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

<%@ 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;
%>




Insert title here


$(document).ready(function(){
	$("tbody>tr").click(function(){
		$(this)
		    .addClass('sel')
		    .siblings().removeClass('sel');
		    .end()
		    .find(':radio').attr('checked',true);
	});
})


.sel{
  background: red;
}



 
  
    
	     
	     
    
    
       
       
       
    
  
选择
姓名
张三
李四
王五

学习之初对于end的用法不太了解,我把自己理解说下,欢迎指教。

END()用法在官方文档的说明回到最近的一个"破坏性"操作之前。即将匹配的元素列表变为前一次的状态。相信不少人并没有理解它的用法。

举个简单例子

Hello,how are you?

jQuery 代码:

$("p").find("span").end()

其返回的值为:

Hello how are you?

 ]

其实就是:查找P标签中的标签,接着使用END()方法结束对P标签的引用,此时返回的是P标签(JQuery)对象

就如第一个例子如果我采用end()用法,那么写法就是


$(document).ready(function(){
	$("tbody>tr").click(function(){
		$(this)
		    .addClass('sel')
		    .siblings().removeClass('sel');
		$(this).find(':radio').attr('checked',true);
	});
})

如果初始化的时候有表格被选中,则需要处理,代码如下:

$("table :radio:checked").parent().parent().addClass('sel');

或者

$("table :radio:checked").parents("tr").addClass('sel');

或者

$('tbody>tr:has(:checked)').addClass('sel');


本文名称:Jquery之table中根据行选中,进行背景变色和radio选中
URL网址:http://myzitong.com/article/jccehs.html