前端-高潮 jQuery

时间:2021-04-11 06:18:37

jQuery:就是让页面动起来

  参考手册:http://jquery.cuishifeng.cn/

  DOM/BOM/javaScript 继承的类库 《---》模块

一、查找元素

  二、操作元素

  三、实例

一、jQuery的导入

版本:1.x 2.x 3.x 推荐使用1.x版本(兼容浏览器低版本)

   <script src="jquery-1.12.4.js"></script>
  <scirpt>
    $. 或 jquery.来调用方法
  </script>

jquery与dom相互转换:

  jquery对象【0】 --》 dom对象

  dom对象            -->  $(dom对象)

二、查找元素

2.1选择器

1.id选择器
  $('#id')
2.类选择器
$('.class')
3.标签选择器
$('p')
4.属性选择器
$('[alex]') 查找具有alex属性的标签
$('[alex="sb"]') 查找alex=‘sb’属性的标签
     $('input[type="text"]') 超找input标签中text的标签
5.组合选择器
     $(a,.class,#id)      以,隔开
6.层级选择器
     $(div a) 子子孙孙,div下的a标签
     $(div>a)          只找儿子
7.基本过滤
     :first 第一个
     :eq   索引0开始
     :odd   偶数

2.2 筛选器

根据选择器选的东西,进行筛选

    parent()      父标签
      parents()   输出所有的父亲的标签
      parentsUntil() 输出所有的父亲标签,直到什么为止
children()    所有的子标签
next()   下一个标签
      nextall()   找到下面全部
      nextUntil()  找到下面某一个为止 
perv()   上一个标签
siblings() 所有的兄弟便签,除了自己
  
  eq()       根据索引来查找
  first()      第一个
  last()       最后一个
  hasClass()    有该属性

左侧菜单jquery实现

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.menu{
width: 80px;
border: 1px solid #ddd;
}
.header{
border: 1px solid #dddddd;
height: 30px;
background-color: #2459a2;
}
.hide{
display: none;
}
</style>
</head>
<body> <div class="menu">
<div class="item">
<div class="header">菜单1</div>
<div class="content">内容1</div>
</div>
<div class="item">
<div class="header">菜单2</div>
<div class="content hide">内容2</div>
</div>
<div class="item">
<div class="header">菜单3</div>
<div class="content hide">内容3</div>
</div>
<div class="item">
<div class="header">菜单4</div>
<div class="content hide">内容4</div>
</div>
</div>
<script src="jqure/jquery-1.12.4.js"></script>
<script>
$('.header').click(function(){
//this 当前点击的便签对象
//要是jquery 必须先进行转换
// $(this).next().removeClass('hide');
// $(this).parent().siblings().children('.content').addClass('hide')
//支持链式编程
$(this).next().removeClass('hide').parent().siblings().children('.content').addClass('hide');
//$(this).next().removeClass('hide').parent().siblings().find('.content').addClass('hide');
})
</script>
</body>
</html>

左侧菜单case

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input id='i1' type="button" value="全选">
<input id="i2" type="button" value="取消">
<input id="i3" type="button" value="反选">
<table id='tb' border="1px">
<tr>
<th>选择</th>
<th>主机</th>
<th>IP</th>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>1.1.1.1</td>
<td>888</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>1.1.1.1</td>
<td>888</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>1.1.1.1</td>
<td>888</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>1.1.1.1</td>
<td>888</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>1.1.1.1</td>
<td>888</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>1.1.1.1</td>
<td>888</td>
</tr>
</table>
<script src="jquery-1.12.4.js"></script>
<script>
//使用jquery的each循环方法
//this 是代表每一个被过滤出来的对象(dom对象)
//$(dom) 进行转换
// $('#i1').click(function() {
// $('#tb :checkbox').each(function(k) {
// //console.log(this) 查看this的属性
// //console.log($(this))
//
// //dom的实现方式
//// if(this.checked){
//// this.checked=false;
//// }
//
// //第二种jquery的方法
// $(this).prop('checked',true)
// })
// });
$('#i1').click(function(){
//内部自己帮我们进行循环
$('#tb :checkbox').prop('checked',true);
});
$('#i2').click(function(){
$('#tb :checkbox').prop('checked',false); });
$('#i3').click(function(){
//进行反选的时候,无法使用简单的内部循环,so我们使用each方法
//each的k代表索引
$('#tb :checkbox').each(function () {
var tag=$(this).prop('checked')?false:true;
//使用三元运算判断当前的的选择状态
$(this).prop('checked',tag)
})
}) </script>
</body>
</html>

全选 反选 取消

二、操作元素

2.1文本操作

$(..).text()        获取文本
$(..).text(..)  设置文本 $(..).html()
$(..).html(..)
$(..).val()    
$(..).val(..) 设置value

2.2 样式操作

addClass()
removeClass()
hasClass()
toggleClass() 其实就是对上面的封装
        <input type="button" value="开关">
<div class="i1 hide">dedewfqf</div>
<script src="jqure/jquery-1.12.4.js"></script>
<script>
$('input').click(function () {
// if($('.i1').hasClass('hide')){
// $('.i1').removeClass('hide')
// }else {
// $('.i1').addClass('hide')
// }
$('.i1').toggleClass('hide')
})
</script>

开关灯case

2.3 属性操作

#对自定义的样式进行操作
$(..).attr(n)        获取该属性设置的值
$(..).attr(n,v)      设置该属性
$(..).removeAttr(n)    删除该属性
  在jquery中1.x 2.x    中对checkbox radio使用该方法会出错,兼容性问题 #专门对checkbox radio选中的操作
$(..).prop('checked')       返回选中状态
$(..).prop('checked',‘true’)  设置选中

index() 索引操作

#css中style的操作
$('t1').css('样式名称', '样式值')
 

2.3 文档处理

#添加
append 在后面追加
prepend   在前面加
after    平级后加
before    平级前加 #删除
remove 移除标签
empty    只清空内容 #克隆
clone

 2.4 位置操作

滑动滚轮与滚轮当前的位置
1 $(window).scrollTop(); 获取
$(window).scrollTop();   设置并移动
3 $(window).scrollLeft(); 类似上面 位置操作
$(window).offset()
$('div').offset() 获取到的是一个数组
$('div').offset().top     获取数组中top
$('div').offset().left      获取数组中left
    offset 是标签在文档中(可见窗口)的位置
postion()               是针对父标签的位置 如果对absolute标签,那他的父级是第一个relative标签(不管其他的层数)  $(..).height()       标签真实高度
$(..).innerHeight()     真实高度+padding
$(..).outerHeight()    真实高度+padding+border
$(..).outerHeight(true)  真实高度+padding+border+margin

2.5事件

绑定事件

 ('..').click(function(){})
区别dom的 都没有on ('..').bind(‘click’,function(){})
('..').unbind(‘click’,function(){})
为事件解绑 $('.c').delegate('a', 'click', function(){})
$('.c').delegate('a', 'click', function(){})
deletage 有个特殊的使用方法
在html中,添加元素之后,无法进行绑定。使用该方法就可以动态绑定 ********************************
上面的三种方法都是基于on 与 off封装的
$('.c1').on('click', function(){})
$('.c1').off('click', function(){})

阻止事件发生

  当事件的返回值为flase时,另外一个事件就不会执行

  在dom中,绑定事件的时候需要加一个return

  在jquerey中,直接在函数中return就可以了

前端-高潮 jQuery

案例:

  当前页面  未连接 是红色字体,需要改成正常,颜色为black

$('font:contains("未连接")').text('正常')
$('font:contains("正常")').css({'color':'black'})
$('span:contains("2018")').text('2018-03-16')