CSS优先级问题以及jQuery中的.eq()遍历方法和:eq()选择器的差别
在写一个TAB选项卡的时候遇到几个有意思的问题,记录下来
先把代码贴出来
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>TAB选项卡</title>
<style>
*{padding: 0;margin: 0;}
body{font: normal 14px/1.5em Arial}
#center{margin: 0 auto;width: 300px;position: relative;top: 50px;}
.tab li{width: 50px;height: 25px;font: normal 12px/25px Arial;float: left;background: #F1F1F1;text-align: center;border: 1px solid #ccc;list-style: none;margin-right: 5px;cursor: pointer;
-moz-border-radius: 15% 15% 0 0;
-webkit-border-radius: 15% 15% 0 0;
border-radius: 15% 15% 0 0;
/*IE 7 AND 8 DO NOT SUPPORT BORDER RADIUS*/
}
.tab_box{width: 300px;height: 200px;border: 1px solid #ccc;clear: both;}
.tab li.hover{background: #C0C0C0}
.tab li.selected{color: #fff;background: #6D84B4}
.hide{display: none;}
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.tab ul li').click(function() {
$(this).addClass('selected')
// 元素高亮
.siblings().removeClass('selected');
//去掉同辈元素高亮
var index = $('.tab ul li').index(this);
//获取当前点击的<li>元素在全部 li 元素中的索引
$('div.tab_box > div')
//选取子节点
.eq(index).show()
.siblings().hide();
}).hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
}); });
</script>
</head>
<body>
<div id="center">
<div class="tab">
<ul>
<li class="selected">时事</li>
<li>体育</li>
<li>娱乐</li>
</ul>
</div>
<div class="tab_box">
<div>时事</div>
<div class="hide">体育</div>
<div class="hide">娱乐</div> </div>
</div>
</body>
</html>
一、CSS优先级的问题。
这是效果图:
按照上述代码,选中状态下,鼠标放在‘体育’的 选项上,是没有hover效果的。
这是一个CSS优先级的问题。把
.tab li.hover{background: #C0C0C0}
.tab li.selected{color: #fff;background: #6D84B4}
这两个调换一下顺序
.tab li.selected{color: #fff;background: #6D84B4}
.tab li.hover{background: #C0C0C0}
就会有这种效果:
由此引出一个CSS优先级的问题,贴一篇文章,可以参考下http://spartan1.iteye.com/blog/1526735
二、eq()遍历方法和:eq()选择器的区别
在上述代码中,可以直接使用.eq(index),
若要使用eq选择器则要写成:eq(‘+index+’)这样获取到的index才是变量。
这个问题有点意思,也debug了很久才查出来了问题…
jQuery推荐使用的方法是.eq(index),相关讨论可以参考:
http://stackoverflow.com/questions/10343150/the-jquery-eqindex-selector
还有一篇不错的选择器进阶文章:http://www.qingdou.me/2344.html
CSS优先级问题以及jQuery中的.eq()遍历方法和:eq()选择器的差别的更多相关文章
- jQUery中的$(document).ready()方法和window.onload()方法的区别
1.常规的Javascript代码中,通常使用window.onload方法 window.onload = function(){//代码} 2.jquery中,则使用$(document).rea ...
- jQuery中focusin()和focus()、find()和children()的差别
jQuery中focus()和focusin().focus()和children()的差别 focus()和focusin() focus()和focusin()的差别在于focusin()支持事件 ...
- jQuery中的append()和prepend(),after()和before()的差别
jQuery中的append()和preappend(),after()和before()的差别 append()和prepend() 如果 <div class='a'> //<- ...
- Object类中wait代餐方法和notifyAll方法和线程间通信
Object类中wait代餐方法和notifyAll方法 package com.yang.Test.ThreadStudy; import lombok.SneakyThrows; /** * 进入 ...
- JS和jQuery中ul li遍历获取对应的下角标
首先先看代码: html代码部分: <div id="div"> <ul> <li>1111111</li> <li>2 ...
- jQuery中使用$.each()遍历数组时要注意的地方
使用jQuery中 $.each()遍历数组,要遍历的数组不能为空(arry!="") 例如: $.each(arry, function (i, item) ...
- 写的非常好的文章 C#中的委托,匿名方法和Lambda表达式
简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个Fir ...
- jquery中的index方法和eq方法
jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数,如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的位 ...
- jquery 中 eq()遍历方法 和:eq()选择器的区别
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
随机推荐
- shell 将字符串作为变量名并打印
使用shell的eval实现此功能.代码如下: #!/bin/sh IP9="127.0.0.1" i=9 eval echo \$IP${i} #!/bin/sh WEBIP0= ...
- Hadoop 介绍
1.Hadoop简介 Hadoop[hædu:p]实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高容错性的特点,并且设计用来部署在低 ...
- Type.IsAssignableFrom 方法
方法: public virtual bool IsAssignableFrom ( Type c ) 1.如果 c 和当前 Type 表示同一类型2.当前 Type 在 c 的继承层次结构中3.当前 ...
- Jmeter-7-在命令行中运行Jmeter.
jmeter -n -t D:\Jmeter_result\Script_baidu.jmx -l D:\Jmeter_result\Script_baidu.txt jmeter -n -t D:\ ...
- POj 2104 K-th Number (分桶法+线段树)
题目链接 Description You are working for Macrohard company in data structures department. After failing ...
- Spring Boot中配置文件application.properties使用
一.配置文档配置项的调用 启动后在浏览器直接输入http://localhost:18080/user/test,就直接打印出配置文件中的配置内容. 二.绑定对象bean调用 有时候属性太多了,一个个 ...
- Android控件——ImageView
android:orientation="vertical" 修改布局文件垂直排列 放置图片: 1.通过src引入图片: 2.通过background引入背景图片 3.baco ...
- Perl6 Bailador框架(6):获取用户输入
use v6; use Bailador; get '/' => sub { ' <html> <head><title></title>< ...
- mysql 之修改初始密码
转载自:https://www.cnblogs.com/ivictor/p/5142809.html 为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于er ...
- 【OneNote】使用线性格式输入数学公式
在OneNote中按Alt+=,就可以开始输入公式. # 对齐公式数组 可以使用@和&来实现,如 \eqarray(x+1&=2@1+2+3+y&=z@3/x&=6)& ...