jQuery之必会增删改查Dom操作
.next .prev
<button>change</button>
<span class = '.demo'>aaa</span>
<p class = '.demo'>bbb</p>
<script src="./jquery.js"></script>
<script>
$('button').click(function(){
$(this).next().css({fontSize:'20px',color:'orange'})
})
// next中可以传参数 如:next('p') 如果下一个兄弟元素节点不是p则不显示
//prev 同理 next
nextAll( ) prevAll( )
nextAll
<div class="wrapper">
全选:<input type="checkbox"></input>
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox">
<input type="submit" value = "login"></input>
</div>
<script src = "./jquery.js"></script>
<script>
$('input[type="checkbox"]').eq(0).click(function(){
if( $(this).prop('checked')){
$(this).nextAll().prop('checked',true)
}else{
$(this).nextAll().prop('checked',false)
}
})
//(在控制台输出) $('input:last').prop('checked') 返回true
//但是最后submit不需要被选择 所以在nextAll中需添加'input[type="checkbox"]'
$('input[type="checkbox"]').eq(0).click(function(){
if( $(this).prop('checked')){
$(this).nextAll('input[type="checkbox"]').prop('checked',true)
}else{
$(this).nextAll('input[type="checkbox"]').prop('checked',false)
}
})
</script>
nextUntil 直到什么为止
<div class="wrapper">
<h1>水果</h1>
全选:<input type="checkbox"></input>
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox"> <h1>nba</h1>
全选:<input type="checkbox"></input>
Rose:<input type="checkbox">
Curry:<input type="checkbox">
James:<input type="checkbox">
<input type="button" value = "submit">
</div>
<script src = "./jquery.js"></script>
<script>
$('h1').next().click(function(){
if( $(this).prop('checked')){
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',true)
}else{
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',false)
}
});
//nextUntil两个参数 第一个表示到哪为止 第二个是限定条件,也就是找哪个属性
</script>
小练习
<div class="wrapper">
all: <input type="checkbox"></input>
<h1>吃货清单</h1>
all: <input type="checkbox"></input>
<h2>水果</h2>
全选:<input type="checkbox"></input>
banana:<input type="checkbox">
apple:<input type="checkbox">
orange:<input type="checkbox"> <h2>蔬菜</h2>
全选:<input type="checkbox"></input>
tomato:<input type="checkbox">
egg:<input type="checkbox">
potato:<input type="checkbox">
<h2></h2>
<h1>明星清单</h1>
all: <input type="checkbox"></input>
<h2>nba</h2>
全选:<input type="checkbox"></input>
Rose:<input type="checkbox">
Curry:<input type="checkbox">
James:<input type="checkbox">
</div>
<script src = "./jquery.js"></script>
<script>
$('input').eq(0).click(function(){
if( $(this).prop('checked')){
$(this).nextAll('input[type="checkbox"]').prop('checked',true)
}else{
$(this).nextAll('input[type="checkbox"]').prop('checked',false)
}
})
$('h1').next().click(function(){
if($(this).prop('checked')){
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',true)
}else{
$(this).nextUntil('h1','input[type="checkbox"]').prop('checked',false)
}
})
$('h2').next().click(function(){
if($(this).prop('checked')){
$(this).nextUntil('h2','input[type="checkbox"]').prop('checked',true)
}else{
$(this).nextUntil('h2','input[type="checkbox"]').prop('checked',false)
}
})
</script>
siblings
<ul>
<span>span1</span>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<span>span2</span>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<span>span3</span>
<li>9</li>
<li>10</li>
</ul>
<script src = "./jquery.js"></script>
<script>
$('li').eq(4).css('backgroundColor','red').siblings().css('backgroundColor','orange')
//所以兄弟元素节点
$('li').eq(4).css('backgroundColor','red').siblings('span').css('backgroundColor','orange')
//传参代表过滤 (只选span标签) </script>
parent()(上一级父级) 可以不传参 传参为过滤条件
<!-- <div class="shop" data-id = "101">
<p>basketball-nike</p>
<button>add</button>
</div>
<div class="shop" data-id = "102">
<p>basketball-adidas</p>
<button>add</button>
</div> --> <div class="wrapper"> </div>
<script src = "./jquery.js"></script>
<script>
var showArrs = [
{
name:'nike',
id:'101'
},{
name:'adidas',
id:'102'
}
]; //把数据写成上面注释的形式
var str = '';
showArrs.forEach(function(ele,index){
str += '<div class="shop" data-id=" '+ ele.id +' "><p> '+ ele.name +'</p><button>add</button></div>';
});
$('.wrapper').html(str);
var carArr = [];
$('button').click(function(){
carArr.push( $(this).parent().attr('data-id') );
});
</script>
parents( ) 获取多个父级 可以不传参 也可以传参过滤
closest 离你最近的满足条件的父级
父级重复可以使用closest 找到最近的父级
offsetParent
<style>
.wrapper{
position:absolute;
}
</style>
<body>
<div class="wrapper">
<div class="box">
<span>123</span>
</div>
</div>
<script src = "./jquery.js"></script>
<script>
console.log($('span').offsetParent());//找离他最近有定位的父级 (是wrapper)
</script>
.slice() 截取
insertBefoe( )、 before( )
insertAfter( )、after( )
appendTo( ) append( )
prependTo( ) prepend( )
<style>
.wrapper{
border:1px solid black;
width:200px;
padding:10px;
}
.wrapper div{
width:200px;
height:100px;
}
.wrapper .box1{
background:red;
}
.wrapper .box2{
background:orange;
}
.content{
width:200px;
height:50px;
background:blue;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box1">box1</div>
<div class="box2">box2</div>
</div>
<div class="content">content</div>
<script src = "./jquery.js"></script>
<script>
$('.content').insertBefore('.box1');//插入content在box1的前面
$('.box1').before($('.content'));//填在before里的内容在前面
//两个方法结果相同 选用哪个取决于想对哪个元素进行链式操作 第一种对.content进行操作,第二种对box1操作
//insertAfter( )、after( )用法相同 $('.content').appendTo('.wrapper');//把前面的添加到后面的里
$('.wrapper').append( $('.content') );//意思与上面相同 $('.wrapper').prepend( $('.content') );
$('.content').prependTo('.wrapper'); //在什么前面添加 </script>
remove( ) detach( )
<style>
div{
width:100px;
height:100px;
background:orange;
}
</style>
</head>
<body>
<div></div>
<script src = "./jquery.js"></script>
<script>
$('div').click(function(){
alert(0);
})
//控制台中输出:
$('div').remove().appendTo( $('body') ) //将div删除再添加回来,原来的点击事件也没了
$('div').detach().appendTo( $('body') )//原来的点击事件保留
</script>
$( ) 参数:标签字符串 可以创建元素
<style>
div{
width:100px;
height:100px;
background:orange;
}
</style>
</head>
<body> <script src = "./jquery.js"></script>
<script>
$('<div></div>').appendTo( $('body'));//可以创建标签
$('<div><span style="color:red;">aaa</span></div>').appendTo( $('body'));//可以创建复杂的
$('<div><span style="color:red;">aaa</span></div><p></p>').appendTo( $('body'));//同级的标签(p)也可以放在后面
</script>
.wrap( ) .wraplnner( ) wrapAll unWrap
.wrap( )
<div class="demo"></div>
<div class="box"></div>
<script src = "./jquery.js"></script>
<script>
//wrap 创建父级
$('.demo').warp('<div></div>');//参数可以传字符串这种创建对象的形式
$('.demo').warp('<div class="wrapper"></div>');//可添加属性
$('.demo').warp( $('<div class="wrapper"></div>') );//也可创建对象这种形式
$('.demo').warp( $('.box') );//选中已有的标签是复制
//wrap() 也可填函数
.wraplnner( )
<div class="demo">
<span></span>
<span></span>
</div>
<script src = "./jquery.js"></script>
<script>
//wrapInner 内部标签加父级
$('.demo').wrapInner('<div/>'); //demo里的span就会被div包裹住
//也可以是函数的形式
$('.demo').wrapInner(function(index){
return '<div class="wrapper' + index + '"></div>'
});
</script>
wrapAll 统一加上父级 wrap是分别加上 (比如有两个class为demo 的div 统一给div里的元素加上一个父级 用wrap就是分别给两个div里的元素加一个父级)
unWrap 把直接父级删掉 一直调用一直往上删 (到body为止)
.clone( )
jQuery之必会增删改查Dom操作的更多相关文章
- jQuery调用WebService实现增删改查的实现
第一篇博客,发下我自己写的jQuery调用WebService实现增删改查的实现. 1 <!DOCTYPE html> 2 3 <html xmlns="http://ww ...
- 使用DOM进行xml文档的crud(增删改查)操作<操作详解>
很多朋友对DOM有感冒,这里我花了一些时间写了一个小小的教程,这个能看懂,会操作了,我相信基于DOM的其它API(如JDOM,DOM4J等)一般不会有什么问题. 后附java代码,也可以下载(可点击这 ...
- mysql增删改查相关操作
mysql增删改查相关操作 以前用mysql用的少,对于数据库相关的操作不熟悉,现在开始要接触数据库了,记录一下相关的基础操作吧. 1.数据库的授权操作 # mysql -u root -p Ente ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
- BaseDao代码,用于连接数据库实行增删改查等操作
在学习JavaWeb时会用到此代码,用于实行增删改查操作 1 package com.bdqn.dao; import java.sql.Connection; import java.sql.Dri ...
- android139 360 黑名单 增删改查-数据库操作
BlackNumberOpenHelper.java package com.itheima52.mobilesafe.db.dao; import android.content.Context; ...
- [Android] SQLite数据库之增删改查基础操作
在编程中常常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,可以支持Windows/Linux/Un ...
- MySQL常用增删改查等操作语句
修改数据库的字符集 mysql>use mydb mysql>alter database mydb character set utf8;创建数据库指定数据库的字符集 ...
- IDEA对数据库、表、记录的(增删改查可视化操作)、数据库安全性问题的演示
对数据库的增删改查 新增数据库 修改数据库 删除数据库 对表的增删改查 新增表 修改表 删除表 对记录的增删改查 数据库安全性问题的演示 演示脏读 一个事物里面读到了另外一个事物没有提交的数据: ...
随机推荐
- Centos6.5修改镜像为国内的阿里云源
第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back ...
- Flask+uwsgi+virtualenv环境配置
Linux系统版本: SLES12sp3 (阿里云) 1. 首先需要安装python-devel,否则后续安装会报错! rpm -qa|grep python-base 结果: python-base ...
- java.lang.ClassNotFoundException的解决方法
java.lang.ClassNotFoundException的解决方法 出现这个问题的原因可能很多,但是最终原因都是部署的项目文件中没有这个类包. 那么出错的点在哪呢?逐一排除! 1.首先在项 ...
- 初学java集合框架笔记
List接口常用方法: 方法名 说 明 boolean add(Object o) 在列表的末尾顺序添加元素, 起始索引位置从0开始 void add(int index,Object o) 在 ...
- 为什么要将Apache与Tomcat集成?(或不)
Why should I integrate Apache with Tomcat? (or not) There are many reasons to integrate Tomcat with ...
- _proto_ && prototype (原型 && 原型链)
原型一直都是JavaScript基础里面的痛点,因为在JavaScript里面没有类的概念,都是通过原型对象来实现继承,下面的这个图很好的说明几者之间的关系! a.__proto__ = A.prot ...
- 2018.4.24 快排查找第K大
import java.util.Arrays; /* 核心思想:利用快排思想,先假定从大到小排序,找枢纽,枢纽会把大小分开它的两边,当枢纽下标等于k时, 即分了k位在它左边或右边,也就是最大或最小的 ...
- 细谈getRequestDispatcher()与sendRedirect()的区别
问题?细谈getRequestDispatcher()与sendRedirect()的区别 首先我们要知道: (1)request.getRequestDispatcher()是请求转发,前后页面共享 ...
- 使用rpm 打包开发的postgres extension
环境准备 安装依赖包 rpmdevtools rpm-build yum install -y rpm-build rpmdevtools 初始化rpm pacakge 项目 主要是rpm 打包的 ...
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...