博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery – 3.JQuery的Dom操作
阅读量:7035 次
发布时间:2019-06-28

本文共 2260 字,大约阅读时间需要 7 分钟。

   

  

   

  

  

  

   

   

   

  

  

   

 

3.1 JQuery的Dom操作


1、使用html()方法读取或者设置元素的innerHTML:

alert($("#btn1").html());
$("#btn1").html("hello");
2、使用text()方法读取或者设置元素的innerText:
alert($("#btn1").text());
$("#btn1").text("hello");
3、使用attr()方法读取或者设置元素的属性,对于JQuery没有封装的属性(所有浏览器没有差异的属性)用attr进行操作。
alert($(“#btn1").attr("href"));
$("#btn1").attr("href", "http://www.cnblogs.com/tangge”);
4、使用 removeAttr 删除属性。删除的属性在源代码中看不到,这是和清空属性的区别。“查看源文件”只能看服务器上下载下来的那份。

显示行号 复制代码 这是一段程序代码。
 
 
 
$(function () {
$("#btn").click(function () {
$("#link").attr("href", "http://www.baidu.com");
});
$("#btn_move").click(function () {
//获取属性值
//$("#link").attr("href");
 
//删除属性值
$("#link").removeAttr("href");
});
}
)
 
 
 
BaiDu
 
 
 

image3

 

案例:图片浏览器

显示行号 复制代码 这是一段程序代码。
 
 
 
$(function() {
var link = $("ul a");
link.click(function () {
//this 触发事件的当前a标签  Dom对象
// $(this).attr("href")
 
$("#i1").attr("src", this.href);
return false;
});
 
})
 
 
 
  • 美女1
  • 美女2
  • 美女3
  • 美女4
  •  
     
     

    对比 开始的【 DOM版:】

     

     

    节点遍历


    1.next()方法用于获取节点之的挨着的第一个同辈元素,$(".menuitem").next("div")、nextAll()方法用于获取节点之后的所有同辈元素,$(".menuitem").nextAll("div")

    2.prev、prevAll兄弟中之前的元素。

    3.siblings()方法用于获取所有同辈元素,$(".menuitem").siblings("li")。siblings、next等所有能传递选择器的地方能够使用的语法都和$()语法一样。

    4.end()将匹配的元素列表变为前一次的状态。

    //end() 返回上一次包装集被破坏之前的状态
    $("#d4").nextAll().css("background-color", "blue").end().css("background-color",
    "red");

     

    5.andSelf()加入先前所选的加入当前元素中

    6.案例:横向菜单,选中的项高亮显示 $(this).css();$(this).siblings().css()

     
     
     
     
    *
    {
    margin:0;
    padding:0;
    }
    #menu
    {
    list-style-type:none;
    margin-top:50px;
    margin-left:100px;
    }
    #menu li
    {
    float:left;
    width:100px;
    height:30px;
    line-height:30px;
    background-color:Gray;
    text-align:center;
    cursor:pointer;
    }
     
    $(document).ready(function () {
     
    $("#menu li").click (function () {
    $(this).css("background-color", "red").siblings().css("background-color", "Gray");
    })
    })
     
     
     
  • 首页
     
  • 播客
     
  • 相册
     
  • 关于
     
     
     
     
     

    image

    7.案例:评分控件。prevAll,this,nextAll

     

    显示行号 复制代码 这是一段程序代码。
     
     
     
     
    *
    {
    margin:0;
    padding:0;
    }
    #rating
    {
    list-style-type:none;
    margin:50px 100px;
    }
    #rating li
    {
    float:left;
    width:20px;
    text-align:center;
    cursor:pointer;
    }
     
    $(function () {
    $("#rating li").mouseover(function () {
    //alert(1);
    $(this).prevAll().andSelf().css("color", "red").end().end().nextAll().css("color", "black");
     
    //分开写(上面用链式编程)
    //$(this).prevAll().andSelf().css("color", "red");
    //$(this).nextAll().css("color", "black");
    })
    })
     
     
     
    •  
    •  
    •  
    •  
    •  
       
       
       

      image

      你可能感兴趣的文章
      使用SublimeText2写OSC博客
      查看>>
      C# 中datagridview控件的使用
      查看>>
      gson error
      查看>>
      安装Nginx
      查看>>
      Shell中 &>/dev/null和 >/dev/null 2>&1
      查看>>
      Eclipse Memory Analyzer(Java内存泄漏分析工具)
      查看>>
      自定义View 购物车加减数量
      查看>>
      mybatis 打印sql语句
      查看>>
      git项目时遇到的问题
      查看>>
      做一个好的程序猿
      查看>>
      Testlink安装使用
      查看>>
      Android系统性能调优工具介绍
      查看>>
      Greenplum如何激活、同步、删除Standby恢复原始Master
      查看>>
      技术往事:改变世界的TCP/IP协议(珍贵多图、手机慎点)
      查看>>
      减少tcp连接中的time_wait
      查看>>
      国内一款仅需150M内存的开源JAVA企业网站系统-MiinE
      查看>>
      基于maven使用IDEA创建springboot多模块项目
      查看>>
      才上线的第一个iphone app私人相簿(加密保护您的隐私),请大家支持下
      查看>>
      资源下载地址
      查看>>
      gnu autotools
      查看>>