JQuery学习三(隐式迭代和节点遍历)
在JQuery中根据id获取控件,如果输入id错误是不报错的。
必要时可以通过写判断语句进行判断是否id写错
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery</title>
<style type="text/css">
.warning {
background-color:yellow;
}
</style>
<script src="js/jquery-1.11.1.min.js"type="text/javascript"></script>
<script type="text/javascript">
// $(function(){$('#btn').mouseover(function () { alert("鼠标在我上面!"); })})
//这里的id如果错误就不会报错。可以自己写出控制是否报错
$(function () {
var elements = $('#btn');
if (elements.length <= 0) {
alert("报错");
return;
}
elements.mouseover(function () { alert('鼠标在我上面');});
})
</script>
</head>
<body bgcolor="blue">
<input value="点击"type="button" id="btn"/>
</body>
</html>
.next方法用于获取本节点后面第一个同辈的节点。
意思是与本节点在同一层次级别中的下一个节点对应的值
所以说next就是指向下一个。(这里面用到的this是一个dom对象,需要转换成jquery对象)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery</title>
<script src="js/jquery-1.11.1.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('p').click(function () { alert($(this).next().text()); });
//注意这里this是dom对象,要强制转换成jquery对象
})
</script>
</head>
<body bgcolor="blue">
<p>aa</p>
<p>bb</p>
<div>div</div>
<p>cc</p>
<p>dd</p>
</body>
</html>
nextAll()是指本节点后面所有的,方法中还可以加入参数,用来查找哦后面所有相应参数对应的
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery</title>
<script src="js/jquery-1.11.1.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('p').click(function () { alert($(this).nextAll().text()); });
//nextAll('div')
//注意这里this是dom对象,要强制转换成jquery对象
})
</script>
</head>
<body bgcolor="blue">
<p>aa</p>
<p>bb</p>
<div>div</div>
<p>cc</p>
<p>dd</p>
</body>
</html>
《注意是隐式迭代》
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery</title>
<script src="js/jquery-1.11.1.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('p').click(function () { $(this).nextAll('p').css("background","yellow"); });
//nextAll('div')
//注意这里this是dom对象,要强制转换成jquery对象
})
$(function () { $('table td').css("font-size", "60px"); })
$(function () {
$('table td').mouseover(function () {
$('table td').css("color", "red");
$(this).nextAll('td').css("color", "black"); })
})
</script>
</head>
<body bgcolor="blue">
<p>aa</p>
<p>bb</p>
<div>div</div>
<p>cc</p>
<p>dd</p>
<table>
<tr>
<td>★</td>
<td>★</td>
<td>★</td>
<td>★</td>
<td>★</td>
</tr>
</table>
</body>
</html>
siblings()获取所有同辈元素
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JQuery</title>
<script src="js/jquery-1.11.1.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(function () { $('table td').css("font-size", "60px"); })
$(function () {
$('td').click(function () {
$(this).css("background", "red");
$(this).siblings("td").css("background","blue");
})
})
</script>
</head>
<body bgcolor="blue">
<table>
<tr>
<td>★</td>
<td>★</td>
<td>★</td>
<td>★</td>
<td>★</td>
</tr>
</table>
</body>
</html>
JQuery学习三(隐式迭代和节点遍历)的更多相关文章
- js的事件循环绑定和jQuery的隐式迭代
js的事件循环绑定和jQuery的隐式迭代 js事件循环绑定 jQuery隐式迭代 先举一个例子:给定一个ul,点击列表内的每一个li元素,使它的背景色变红,下边分别用js代码和jQuery实现. & ...
- jQuery关于隐式迭代的个人理解~
1.JQuery对象" 如: $('div').text("div展示的信息") 可以看成"是一个包含一个dom数组 和 包含所有Jquery方法的容器 2.每 ...
- JQuery的链式编程,隐式迭代是啥意思?
链式编程 1.好处 "一句话,链式编程可以省去很多重复的代码." 这话什么意思呢?举个例子. /*设置obj对象的两个属性*/ //普通做法是这样的 obj.name = '小明' ...
- jQuery——链式编程与隐式迭代
链式编程 1.原理:return this; 2.通常情况下,只有设置操作才能把链式编程延续下去.因为获取操作的时候,会返回获取到的相应的值,无法返回 this. 3.end():结束当前链最近的一次 ...
- JQuery的链式编程与隐式迭代
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jquery 点谁谁哭-隐式迭代
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- jQ的隐式迭代和设置样式属性
jQ中的隐式迭代 意义:不需要原生迭代了,在jQ内部自动帮你实现了循环 代码实现: let arr = document.querySelectorAll('li') for(let i = 0;i ...
- jQ的显式迭代和隐式迭代
jQ的显示迭代 隐式迭代 let lis = document.querySelector('li') lis.forEach(function (value, index) { value.styl ...
- JQuery的隐式迭代和each函数和map函数
1.JQuery选择器选择出来的是一个数组对象,可是给这些每一个元素都要设置内容时,就会隐式迭代,JQuery自己实现内部循环给每个元素绑定上设置. 2.如果是获取的话,那就是默认获取第一个元素的值. ...
随机推荐
- Idea Live Templates
常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- Nodejs基础之redis
安装redis 模块 npm install redis 1 代码部分 const redis = require('redis') const client = redis.createClient ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...
- POJ 1679 The Unique MST(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)
Description Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, ...
- Hadoop 版本 生态圈 MapReduce模型
忘的差不多了, 先补概念, 然后开始搭建集群实战 ... . 一 Hadoop版本 和 生态圈 1. Hadoop版本 (1) Apache Hadoop版本介绍 Apache的开源项目开发流程 : ...
- Android 开发 之 JNI入门 - NDK从入门到精通
NDK项目源码地址 : -- 第一个JNI示例程序下载 : GitHub - https://github.com/han1202012/NDKHelloworld.git -- Java传递参数给C ...
- ACM 第十七天
暑期热身赛 BAPC 2014 The 2014 Benelux Algorithm Programming Contest 题目网址:https://odzkskevi.qnssl.com/3655 ...
- 记一次dll强命名冲突事件
一 问题的出现 现在要做一个net分布式平台,平台涉及多个服务之间调用问题,最基础的莫过于sso.由于我们的sso采用了wcf一套私有框架实现,另外一个webapi服务通过接口调用sso服务.由于s ...