09:jQuery(02)
内容概要
jQuery操作标签
jQuery绑定事件
jQuery补充知识点
jQuery动画效果(了解)
- 零散补充
内容详细
jQuery练习题
- $('#i1')
- r.fn.init [div#i1.container]
- $('h2')
- r.fn.init [h2, prevObject: r.fn.init(1)]
-
- $('input')
- r.fn.init(9) [input#exampleInputEmail1.form-control, input#exampleInputPassword1.form-control, input#exampleInputFile, input, input, input, input, input#optionsRadios1, input#optionsRadios2, prevObject: r.fn.init(1)]
- $('.c1')
- r.fn.init(2) [h1.c1, h1.c1, prevObject: r.fn.init(1)]
-
- $('.btn-default')
- r.fn.init [button#btnSubmit.btn.btn-default, prevObject: r.fn.init(1)]
- $('.c1,h2')
- r.fn.init(3) [h1.c1, h1.c1, h2, prevObject: r.fn.init(1)]
-
- $('.c1,#p3')
- r.fn.init(3) [h1.c1, h1.c1, p#p3.divider, prevObject: r.fn.init(1)]
- $('.c1,.btn')
- r.fn.init(11) [h1.c1, h1.c1, a.btn.btn-primary.btn-lg, button.btn.btn-warning, button.btn.btn-danger, button.btn.btn-warning, button.btn.btn-danger, button.btn.btn-warning, button.btn.btn-danger, button#btnSubmit.btn.btn-default, a.btn.btn-success, prevObject: r.fn.init(1)]
- $('form').find('input')
- r.fn.init(3) [input#exampleInputEmail1.form-control, input#exampleInputPassword1.form-control, input#exampleInputFile, prevObject: r.fn.init(1)]
- $('label input')
- r.fn.init(6) [input, input, input, input, input#optionsRadios1, input#optionsRadios2, prevObject: r.fn.init(1)]
- $('label+input')
- r.fn.init(3) [input#exampleInputEmail1.form-control, input#exampleInputPassword1.form-control, input#exampleInputFile, prevObject: r.fn.init(1)]
- $('#p2~li')
- r.fn.init(8) [li, li, li, li, li, li, li, li, prevObject: r.fn.init(1)]
-
- $('#f1 input:first')
- r.fn.init [input#exampleInputEmail1.form-control, prevObject: r.fn.init(1)]
- $('#my-checkbox input:last')
- r.fn.init [input, prevObject: r.fn.init(1)]
-
- $('#my-checkbox input[checked!="checked"]')
- r.fn.init(3) [input, input, input, prevObject: r.fn.init(1)]0: input1: input2: inputlength: 3prevObject: r.fn.init [document]__proto__: Object(0)
-
- $('label:has("input")')
- r.fn.init(6) [label, label, label, label, label, label, prevObject: r.fn.init(1)]
操作标签
- # 操作类
- """
- js版本 jQuery版本
- classList.add() addClass()
- classList.remove() removeClass()
- classList.contains() hasClass()
- classList.toggle() toggleClass()
- """
-
- # css操作
- <p>111</p>
- <p>222</p>
- """一行代码将第一个p标签变成红色第二个p标签变成绿色"""
- $('p').first().css('color','red').next().css('color','green')
- # jQuery的链式操作 使用jQuery可以做到一行代码操作很多标签
- # jQuery对象调用jQuery方法之后返回的还是当前jQuery对象 也就可以继续调用其他方法
- class MyClass(object):
- def func1(self):
- print('func1')
- return self
-
- def func2(self):
- print('func2')
- return self
- obj = MyClass()
- obj.func1().func2()
-
- # 位置操作
- offset() # 相对于浏览器窗口
- position() # 相对于父标签
-
- scrollTop() # 需要了解
- $(window).scrollTop()
- 0
- $(window).scrollTop()
- 969
- $(window).scrollTop() # 括号内不加参数就是获取
- 1733
- $(window).scrollTop(0) # 加了参数就是设置
- n.fn.init [Window]
- $(window).scrollTop(500)
- n.fn.init [Window]
- scrollLeft()
-
- # 尺寸
- $('p').height() # 文本
- 20
- $('p').width()
- 1670
- $('p').innerHeight() # 文本+padding
- 26
- $('p').innerWidth()
- 1674
- $('p').outerHeight() # 文本+padding+border
- 26
- $('p').outerWidth()
- 1674
-
-
- # 文本操作
- """
- 操作标签内部文本
- js jQuery
- innerText text() 括号内不加参数就是获取加了就是设置
- innerHTML html()
-
- $('div').text()
- "
- 有些话听听就过去了,不要在意,都是成年人!
- "
- $('div').html()
- "
- <p>
- 有些话听听就过去了,不要在意,都是成年人!
- </p>
- "
- $('div').text('你们都是我的大宝贝')
- w.fn.init [div, prevObject: w.fn.init(1)]
- $('div').html('你个臭妹妹')
- w.fn.init [div, prevObject: w.fn.init(1)]
- $('div').text('<h1>你们都是我的大宝贝</h1>')
- w.fn.init [div, prevObject: w.fn.init(1)]
- $('div').html('<h1>你个臭妹妹</h1>')
- w.fn.init [div, prevObject: w.fn.init(1)]
- """
- # 获取值操作
- """
- js jQuery
- .value .val()
- """
- $('#d1').val()
- "sasdasdsadsadad"
- $('#d1').val('520快乐') # 括号内不加参数就是获取加了就是设置
-
- w.fn.init [input#d1]
- $('#d2').val()
- "C:\fakepath\01_测试路由.png"
- $('#d2')[0].files[0] # 牢记两个对象之间的转换
- File {name: "01_测试路由.png", lastModified: 1557043083000, lastModifiedDate: Sun May 05 2019 15:58:03 GMT+0800 (中国标准时间), webkitRelativePath: "", size: 28733, …}
- # 属性操作
- """
- js中 jQuery
- setAttribute() attr(name,value)
- getAttribute() attr(name)
- removeAttribute() removeAttr(name)
-
- 在用变量存储对象的时候 js中推荐使用
- XXXEle 标签对象
- jQuery中推荐使用
- $XXXEle jQuery对象
- """
- let $pEle = $('p')
- undefined
- $pEle.attr('id')
- "d1"
- $pEle.attr('class')
- undefined
- $pEle.attr('class','c1')
- w.fn.init [p#d1.c1, prevObject: w.fn.init(1)]
- $pEle.attr('id','id666')
- w.fn.init [p#id666.c1, prevObject: w.fn.init(1)]
- $pEle.attr('password','jason123')
- w.fn.init [p#id666.c1, prevObject: w.fn.init(1)]
- $pEle.removeAttr('password')
- w.fn.init [p#id666.c1, prevObject: w.fn.init(1)]
- """
- 对于标签上有的能够看到的属性和自定义属性用attr
- 对于返回布尔值比如checkbox radio option是否被选中用prop
- """
- $('#d3').attr('checked')
- "checked"
- $('#d2').attr('checked')
- undefined
- $('#d2').attr('checked')
- undefined
- $('#d4').attr('checked')
- undefined
- $('#d3').attr('checked')
- "checked"
- $('#d3').attr('checked','checked') # 无效
- w.fn.init [input#d3]
- $('#d2').prop('checked')
- false
- $('#d2').prop('checked')
- true
- $('#d3').prop('checked',true)
- w.fn.init [input#d3]
- $('#d3').prop('checked',false)
- w.fn.init [input#d3]
- # 文档处理
- """
- js jQuery
- createElement('p') $('<p>')
- appendChild() append()
-
- """
- let $pEle = $('<p>')
- $pEle.text('你好啊 草莓要不要来几个?')
- $pEle.attr('id','d1')
- $('#d1').append($pEle) # 内部尾部追加
- $('#d1').append($pEle[0]) # 内部尾部追加
- $pEle.appendTo($('#d1'))
- $('#d1').prepend($pEle) # 内部头部追加
- w.fn.init [div#d1]
- $pEle.prependTo($('#d1'))
- w.fn.init [p#d1, prevObject: w.fn.init(1)]
- $('#d2').after($pEle) # 放在某个标签后面
- w.fn.init [p#d2]
- $pEle.insertAfter($('#d1'))
- $('#d1').before($pEle)
- w.fn.init [div#d1]
- $pEle.insertBefore($('#d2'))
-
- $('#d1').remove() # 将标签从DOM树中删除
- w.fn.init [div#d1]
- $('#d1').empty() # 清空标签内部所有的内容
- w.fn.init [div#d1]
事件
- // 第一种
- $('#d1').click(function () {
- alert('别说话 吻我')
- });
- // 第二种(功能更加强大 还支持事件委托)
- $('#d2').on('click',function () {
- alert('借我钱买草莓 后面还你')
- })
克隆事件
- <button id="d1">屠龙宝刀,点击就送</button>
-
- <script>
- $('#d1').on('click',function () {
- // console.log(this) // this指代是当前被操作的标签对象
- // $(this).clone().insertAfter($('body')) // clone默认情况下只克隆html和css 不克隆事件
- $(this).clone(true).insertAfter($('body')) // 括号内加true即可克隆事件
-
- })
- </script>
左侧菜单
- <script>
- $('.title').click(function () {
- // 先给所有的items加hide
- $('.items').addClass('hide')
- // 然后将被点击标签内部的hide移除
- $(this).children().removeClass('hide')
- })
- </script>
返回顶部
- <script>
- $(window).scroll(function () {
- if($(window).scrollTop() > 300){
- $('#d1').removeClass('hide')
- }else{
- $('#d1').addClass('hide')
- }
- })
- </script>
自定义登陆校验
- # 在获取用户的用户名和密码的时候 用户如果没有填写 应该给用户展示提示信息
- <p>username:
- <input type="text" id="username">
- <span style="color: red"></span>
- </p>
- <p>password:
- <input type="text" id="password">
- <span style="color: red"></span>
- </p>
- <button id="d1">提交</button>
- <script>
- let $userName = $('#username')
- let $passWord = $('#password')
- $('#d1').click(function () {
- // 获取用户输入的用户名和密码 做校验
- let userName = $userName.val()
- let passWord = $passWord.val()
- if (!userName){
- $userName.next().text("用户名不能为空")
- }
- if (!passWord){
- $passWord.next().text('密码不能为空')
- }
- })
- $('input').focus(function () {
- $(this).next().text('')
- })
- </script>
input实时监控
- <input type="text" id="d1">
- <script>
- $('#d1').on('input',function () {
- console.log(this.value)
- })
- </script>
hover事件
- <script>
- // $("#d1").hover(function () { // 鼠标悬浮 + 鼠标移开
- // alert(123)
- // })
- $('#d1').hover(
- function () {
- alert('我来了') // 悬浮
- },
- function () {
- alert('我溜了') // 移开
- }
- )
- </script>
键盘按键按下事件
- <script>
- $(window).keydown(function (event) {
- console.log(event.keyCode)
- if (event.keyCode === 16){
- alert('你按了shift键')
- }
- })
- </script>
补充
阻止后续事件执行
- <script>
- $('#d2').click(function (e) {
- $('#d1').text('宝贝 你能看到我吗?')
- // 阻止标签后续事件的执行 方式1
- // return false
- // 阻止标签后续事件的执行 方式2
- // e.preventDefault()
- })
- </script>
阻止事件冒泡
- <script>
- $('#d1').click(function () {
- alert('div')
- })
- $('#d2').click(function () {
- alert('p')
- })
- $('#d3').click(function (e) {
- alert('span')
- // 阻止事件冒泡的方式1
- // return false
- // 阻止事件冒泡的方式2
- // e.stopPropagation()
- })
- </script>
事件委托
- <button>是兄弟,就来砍我!!!</button>
- <script>
- // 给页面上所有的button标签绑定点击事件
- // $('button').click(function () { // 无法影响到动态创建的标签
- // alert(123)
- // })
- // 事件委托
- $('body').on('click','button',function () {
- alert(123) // 在指定的范围内 将事件委托给某个标签 无论该标签是事先写好的还是后面动态创建的
- })
- </script>
页面加载
- # 等待页面加载完毕再执行代码
- window.onload = function(){
- // js代码
- }
- """jQuery中等待页面加载完毕"""
- # 第一种
- $(document).ready(function(){
- // js代码
- })
- # 第二种
- $(function(){
- // js代码
- })
- # 第三种
- """直接写在body内部最下方"""
动画效果
- $('#d1').hide(5000)
- w.fn.init [div#d1]
- $('#d1').show(5000)
- w.fn.init [div#d1]
- $('#d1').slideUp(5000)
- w.fn.init [div#d1]
- $('#d1').slideDown(5000)
- w.fn.init [div#d1]
- $('#d1').fadeOut(5000)
- w.fn.init [div#d1]
- $('#d1').fadeIn(5000)
- w.fn.init [div#d1]
- $('#d1').fadeTo(5000,0.4)
- w.fn.init [div#d1]
补充
- # each()
- # 第一种方式
- $('div')
- w.fn.init(10) [div, div, div, div, div, div, div, div, div, div, prevObject: w.fn.init(1)]
- $('div').each(function(index){console.log(index)})
- VM181:1 0
- VM181:1 1
- VM181:1 2
- VM181:1 3
- VM181:1 4
- VM181:1 5
- VM181:1 6
- VM181:1 7
- VM181:1 8
- VM181:1 9
-
- $('div').each(function(index,obj){console.log(index,obj)})
- VM243:1 0 <div>•1•</div>•
- VM243:1 1 <div>•2•</div>•
- VM243:1 2 <div>•3•</div>•
- VM243:1 3 <div>•4•</div>•
- VM243:1 4 <div>•5•</div>•
- VM243:1 5 <div>•6•</div>•
- VM243:1 6 <div>•7•</div>•
- VM243:1 7 <div>•8•</div>•
- VM243:1 8 <div>•9•</div>•
- VM243:1 9 <div>•10•</div>•
-
- # 第二种方式
- $.each([111,222,333],function(index,obj){console.log(index,obj)})
- VM484:1 0 111
- VM484:1 1 222
- VM484:1 2 333
- (3) [111, 222, 333]
- """
- 有了each之后 就无需自己写for循环了 用它更加的方便
- """
- # data()
- """
- 能够让标签帮我们存储数据 并且用户肉眼看不见
- """
- $('div').data('info','回来吧,我原谅你了!')
- w.fn.init(10) [div#d1, div, div, div, div, div, div, div, div, div, prevObject: w.fn.init(1)]
- $('div').first().data('info')
- "回来吧,我原谅你了!"
- $('div').last().data('info')
- "回来吧,我原谅你了!"
- $('div').first().data('xxx')
- undefined
- $('div').first().removeData('info')
- w.fn.init [div#d1, prevObject: w.fn.init(10)]
- $('div').first().data('info')
- undefined
- $('div').last().data('info')
- "回来吧,我原谅你了!"
09:jQuery(02)的更多相关文章
- Python开发【第十三篇】:jQuery(二)
http://www.bubuko.com/infodetail-1438296.html 处理完毕需要整理贴进来 Python之路[第十三篇]jQuery案例-Form表单&插件及扩展 ...
- 前端开发:JQuery(2)& Bootstrap
JS事件流 事件的概念:HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件. 事件流: ...
- 前端开发:JQuery(1)
JQuery DOM文档加载的步骤: 1. 解析HTML结构: 2. 加载外部脚本和样式: 3. 解析并执行脚本代码: 4. DOM树构建完成: 5. 加载图片等外部文件: 6. 页面加载完成: JS ...
- 06:JS(02)
对象 一切皆对象 数组(类似于python里面的列表) [] var l = [11,22,33,44,55] typeof l "object" var l1 = [11,'sd ...
- spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(02)
在上篇博客中写道了bean后置处理器InstantiationAwareBeanPostProcessor,只介绍了其中一个方法的作用及用法,现在来看postProcessBeforeInstanti ...
- 08:jQuery(01)
今日内容概要 jQuery(封装了js的前端框架(模块)) 很容易与DOM操作混淆 jQuery """ jQuery内部封装了原生的js代码(还额外添加了很多的功能) ...
- 04:CSS(02)
溢出属性 p { height: 100px; width: 50px; border: 3px solid red; /*overflow: visible; !*默认就是可见 溢出还是展示*!*/ ...
- hadoop系列:zookeeper(2)——zookeeper核心原理(选举)
1.前述 上篇文章<hadoop系列:zookeeper(1)--zookeeper单点和集群安装>(http://blog.csdn.net/yinwenjie/article/deta ...
- iOS开发Swift篇(02) NSThread线程相关简单说明
iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在s ...
随机推荐
- 【DB宝51】CentOS7修改网卡名称
目录 1.修改/etc/default/grub文件 2.修改/etc/udev/rules.d/70-persistent-net.rules文件 3.修改网卡配置文件 4.重启服务器 需求:原来的 ...
- 获取InputStream对象的方法
获取InputStream对象的方法 getResourceAsStream(String path) 默认path路径位于Class所在Module的src目录下 . InputStream is ...
- android 文件存储&SharedPreferences
一.文件存储 文件存储主要是I/O流的操作,没什么好说的,需要注意的是保存文件的各个目录. 下面为常用的目录: public static File getInFileDir(Context cont ...
- 技术博客:Azure Functions + Azure Storage 开发
Azure GitHub wiki 同步发布 传送门 Azure Functions 通过 Functions(一个事件驱动型无服务器计算平台,还可以解决复杂的业务流程问题)更加高效地进行开发.在本地 ...
- SE_Work4_软件案例分析
项目 内容 课程:北航-2020-春-软件工程 博客园班级博客 要求:分析软件案例 个人博客作业-软件案例分析 班级 005 这个作业在哪个具体方面帮助我实现目标 分析对比一类软件,学会规划分析软件的 ...
- queryset惰性与缓存
https://blog.csdn.net/zhu6201976/article/details/83550461
- Let's go!
第一次开通博客 心情还是很激动的,而且做出了这么好看的页面虽然都是用的别人的组件,自己不是很知道原理但是也很开心,以后会将自己学习的东西写成笔记发在上面
- [DB] 关系型数据库
名词 数据库(database):保存有组织的数据的容器,是通过DBMS创建的容器 表(table):某种特定类型数据的结构化清单 元组(tuple):行,一条数据库记录,对应一个事物 属性(prop ...
- [Java] 静态代理 动态代理
原理 静态代理=代理模式 动态代理=代理模式+反射 代理模式可以在不修改被代理对象的基础上,通过扩展代理类,进行一些功能的附加与增强 代理类和被代理类应该共同实现一个接口,或者是共同继承某个类 静态代 ...
- 查阅日志文件:有时候报错信息只是给出了问题的表面现象,要想更深入的了解问题,必须查看相应的日志文件,而日志文件又分为系统日志文件(/var/log)和应用的日志文件,结合这两个日志文件,一般就能定位问题所在。
作为一名合格的 Linux 运维工程师,一定要有一套清晰.明确的解决故障思路,当问题出现时,才能迅速定位.解决问题,这里给出一个处理问题的一般思路: 重视报错提示信息:每个错误的出现,都是给出错误提示 ...