首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有表和鼠标事件的jQuery

问带有表和鼠标事件的jQuery
EN

Stack Overflow用户
提问于 2013-11-26 14:46:13
回答 1查看 385关注 0票数 0

我有一个关于带有表和鼠标事件的jQuery的问题。

我的应用程序是用spring (tiles)和jsp开发的。

我必须显示两个表,第一个表内的第二个表,当我必须处理第二个表时,我想使它影响第一个表,而不影响第二个表。

我有下一个代码: Tiles布局

代码语言:javascript
复制
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!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=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<table id="t1" align="center">
    <tr>
      <td height="30" colspan="2"><tiles:insertAttribute name="header" />
        </td>
    </tr>
    <tr>
        <td height="250"><tiles:insertAttribute name="menu" /></td>
        <td width="350"><tiles:insertAttribute name="body" /></td>
    </tr>
    <tr>
        <td height="30" colspan="2"><tiles:insertAttribute name="footer" />
        </td>
    </tr>
</table>
</body>
</html>

它改变主体的jsp页面

代码语言:javascript
复制
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/style.css"/>">
    <script type="text/javascript" src="<c:url value="/resources/js/jquery-1.10.2.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/resources/js/jquery.tablesorter.js"/>"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('table').tablesorter({ sortList:[[0,0],[1,0]] });
            $("tbody tr").mouseenter(function(){
                $(this).css("background-color", "#CCC");
            });
            $("tbody tr").mouseleave(function(){
                $(this).css("background-color", "#6E6E6E");
            });
        });
    </script>
</head>
<body>
    <table id="t2 "class="tablesorter">
        <thead>
            <tr>
                <th><spring:message code="box.mac" text="default text" /></th>
                <th><spring:message code="box.model" text="default text" /></th>
                <th><spring:message code="box.maker" text="default text" /></th>
                <th><spring:message code="box.serialNumber" text="default text" /></th>
                <th><spring:message code="box.vendor" text="default text" /></th>
                <th><spring:message code="box.purchase" text="default text" /></th>
                <th><spring:message code="box.warranty" text="default text" /></th>
                <th><spring:message code="box.manufacturer" text="default text" /></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1234:5678</td>
                <td>model1</td>
                <td>maker1</td>
                <td>0123456789</td>
                <td>vendor1</td>
                <td>26/11/2013</td>
                <td>26/11/2015</td>
                <td>manufacturer1</td>
            </tr>
            <tr>
                <td>8765:4321</td>
                <td>model2</td>
                <td>maker2</td>
                <td>9876543210</td>
                <td>vendor2</td>
                <td>01/01/2013</td>
                <td>01/01/2015</td>
                <td>manufacturer2</td>
            </tr>
            <tr>
                <td>0000:1111</td>
                <td>model3</td>
                <td>maker3</td>
                <td>1234567890</td>
                <td>vendor3</td>
                <td>01/01/2010</td>
                <td>01/01/2012</td>
                <td>manufacturer3</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

更改是鼠标事件->鼠标中心和带行的mouseleave。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-26 16:39:36

使用children()函数从jQuery (医生来了)只匹配第一个(外部)表。

这个函数的工作方式类似于CSS >选择器,即只选择一个元素的直接子元素,而不是所有的后代。

看看这个例子小提琴

  • 内表<tr>元素具有CSS定义的绿色背景。
  • 外表<tr>具有由jQuery定义的红色背景;这使用children(),因此它只影响表外的<tr>元素。
  • 注释掉的代码不使用children(),因此它影响到所有 <tr>元素,这些元素都是#outer-table的后代。

在您的场景中,您可能希望使用children()来选择外部<tr>元素,并为它们提供所需的事件。

我希望我的例子足够清楚。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20220229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档