欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

HTML 和 Ajax 异步请求中的 jQuery 事件处理 - II,jQuery 使用三部曲

最编程 2024-03-31 07:42:29
...
1、获取元素
  1. ( " 选择器 " ) 查找:找到的是 j Q u e r y 对象(字典),不是 j s 对象 j Q u e r y 转 j s : g e t ( 0 ) j s 转 j Q u e r y : ("选择器") 查找: 找到的是jQuery对象(字典),不是js对象 jQuery转js:get(0) js转jQuery: ("选择器")查找:找到的是jQuery对象(字典),不是js对象jQueryjsget0jsjQuery(js对象)
  2. 遍历:each
    名称.function(index,item){ $(item)}
2、绑定事件
  1. 支持直接使用事件名,也支持多个事件执行
$("span:first-of-type").click(function(e){
            console.log(this,e);
        })
this是js的用法,可用$(this)改为jQuery
  1. 可直接使用on方法,开启事件
    可以传入匿名参数: $(“p”).on(“click”,function(e){
    也可以传入有名参数: $(“.targe”).on(“mouseenter”, event1);
  2. off方法,关闭事件
    可以传入匿名参数
    也可以传入有名参数:$(“.targe”).off(“mouseup”,event2);
    使用有名函数可对同一对象的同一事件进行开关
  3. one:只能执行一次;$(“.targe”).one(“mousedown”, event3);
    • 例:
    • js所有事件:事件名不带on
    • $(this).click(function(e){ })
    • $(this).on(“事件名”,函数名)
    • $(this).off(“事件名”,函数名):不带函数名,则关闭所有事件
    • $(this).one(“事件名”,函数名):只执行一次
3、修改、获取
  1. 内容
    text()
    $(“a”).text()
    html()
    如果没有实参就是获取,有实参就是设置
  2. 属性
    • 普通属性
      attr(“属性名”):获取
      attr(“属性名”,“属性值”):设置
    • 类名属性
      hasClass()
      addClass()
      removeClass()
      toggleClass()
  3. 样式css
    css(“属性名”):获取
    css(“属性名”,“属性值”):获取设置
  4. 动画
    • 显示与隐藏
      • show()
      • hide()
      • toggle()
    • 自定义:animate({ }, time, function(){ })
4、其他
  1. 相关元素

    • 父:parent()
    • 子:childern()
    • 上一个:prev()
    • 下一个:next()
    • 同级别的其他:siblings()
  2. 创建与删除

A.appendTo(B):
- 返回A
- A是创建内容

$("p:nth-of-type(2)").append(
            `<div>
            <a href="https://jd.com">123</a>
            </div>`
        )

A.append(B)
- 返回A
- B是创建内容
( ‘ < d i v c l a s s = " i t e m " > (`<div class="item"> (<divclass="item">{datas[i].project}`).appendTo(“.items”);

目标元素.remove(): 移除
目标元素.empty(): 删除目标元素所有子元素

推荐阅读