初识jQuery获取属性及属性的简单认识(2)

冷不防 2022-04-17 01:57 298阅读 0赞

jQuery的属性及简单的使用

一、属性选择器

1、title 获取属性

  1. //在使用$( )函数时,如果是获取标签,必须使用" "(双引号)引起来,如果是DOM或者变量都不需要使用" "
  2. //title是属性标签
  3. $("[title]");//获取属性是title属性的标签;
  4. $("a[title]");//标签选择器和属性选择器的组合;获取a标签的title属性;
  5. $("[title!=img]")};//选中title属性不是img这个值,也包括没有title属性的元素;

2、class选择器

  1. $("[class^=div]");//获取class名为div开头的
  2. $("[class^='div']");//与上面相同的用途;
  3. $("[class$='0']");//class的值是以0结尾的元素;
  4. $("[class|=lis]");//选中class的值是lis或者值是以lis起头并且后面紧跟-的元素;(如:lis-1;lis-2;lis-8);
  5. $("[class~=bn]");//选中class的类组中包含bn这个类的,也就是说这个类名可能前后有空格;(如:class="bn bn1";class="bn2 bn bn4")
  6. $("[title][class][class!=lis]");//多项属性选择器;

二、筛选器

伪类选择器其实就是筛选器

  1. $("li:first").text("a");//第一个li的文本是"a";
  2. $("li:last").text("a");//最后一个li;
  3. $(".div0:fist");//获取第一个class名是div0的属性;
  4. **//$(".div0:first-of-type");//first-of-child是不支持class名的;**
  5. $("div:first");第一个div元素;
  6. $("div:first-child");//这个必须存在父元素,表示div的第一个子元素;
  7. $(".div0first-child");//这个支持的;表示class名为div0的第一个子元素;
  8. //按奇偶选择(从序列0开始计算)
  9. $("li:even");//列表中偶数序列;
  10. $("li:odd");//列表中奇数序列;
  11. //按索引选择
  12. $("li:eq(1)");//列表中索引值所对应的元素;
  13. $("li:gt(2)");//从列表中索引值是2的向下所有的元素;
  14. $("li:lt(2)");//从列表中索引值是2的向上所有的元素;
  15. $(":header");//h1-h6的元素;
  16. $(":animated");//动画选择器;
  17. $(":focus");//汇集焦距选择器;
  18. $("li").eq(0);// == $("li:eq(0)");
  19. $("li").first()// == $("li:first");
  20. $("li").last();// == $("li:last");
  21. $("li").not(["[class~=div0]"]);// == $("li:not([class~=div0])")
  22. //内容过滤器
  23. $("div:empty");//过滤div是空元素;不含有子元素和内容元素div他的子项中有class是.div0;
  24. $("div:has(.div0)");//找的是父元素,如果某个元素有div0这个class,就会找到他的所有父级元素与$("div:empty")相反;
  25. $("div:parent");//过滤div中是不是空元素的,含有子元素和内容的元素;
  26. $("div").has(".div0");// === $("div:has(.div0)");// 前者效率高;
  27. $("sapn").parent();//仅找到当前选择器的父元素;
  28. $("span").parents();//找到所有父元素;
  29. $("span"),parentsUntil("body");//找到父级元素到某个父元素的所有元素;
  30. $("div:contains('2')");
  31. $("div").contains("2");
  32. //主要是针对display:none来使用
  33. $("div:hidden");//隐藏的元素;
  34. $("div:visible");//显示的元素;
  35. $("li:only-child");//当前项仅为父元素的唯一子项;
  36. $("li:nth-child(2)");//有父元素的li列表中的第2个元素;
  37. $("li:nth-child(even)");//从1开始的偶数项;
  38. $("li:even");//这个是从0开始偶数项;
  39. $("li:nth-child(odd)");//从1开始的奇数项;
  40. $("li:nth-child(3n)");//三的倍数;
  41. $("li:nth-child(2n-1)");//单数
  42. $("li").is(".li0");//判断列表中有没有class是.li0的元素,返回结果是true/false;
  43. $("li").is("#lis");//判断列表中有没有id是lis的元素;
  44. $("li").hasClass("li0");//判断当前列表中有没有class是.li0,只能判断class,函数中不使用.来说明class名;
  45. $("li").slice(2,5);//从jQuery列表中截取对应的元素

三、text、html、value

1、text

  1. $("div").text("aaa");//给所有的jQuery对象都写入aaa的字符;
  2. $("div").text();//获取所有的div中字符;
  3. $("div").each(function (index,elem) {
  4. console.log(index,elem,this);
  5. //这里的this是该DOM对象
  6. })

2、html

  1. $("div").html("<a href='#'>内容</a>");
  2. console.log($("div").html());//这里仅获取第一个html内容;
  3. $.each(arr1,function (index,item) {
  4. $("div").eq(index).html("<a href='"+item.href+"'>"+item.name+"</a>")
  5. })//多个div中添加a标签和数组中的数据;

3、value

  1. $("input").val("aaa");//输入框中含有默认值"aaa"
  2. console.log($("input").val());//获取input的value;

四、属性的设置

设置属性和获取属性

  1. $("div").attr("names","xie");
  2. console.log($("div").attr("names"));
  3. $("div").attr("names",function(index,value){
  4. return "div"+index;
  5. })//给div的names属性加div0,div1,div2……
  6. //$("选择器").attr({
  7. "属性1":function(index,value){
  8. return 值;
  9. },
  10. "属性2":function(index,value){
  11. return 值;
  12. }
  13. })
  14. $("div").attr({
  15. "names":function(index,value){
  16. return "div"+index;
  17. },
  18. "toggle-data":function(index,value){
  19. return "data"+index;
  20. }
  21. })

五、css样式

  1. $("div").css("backgroundColor","red");//设置div背景色是红色;
  2. console.log($("div").css("backgroundColor"));//获取div背景色颜色;
  3. $("div").css("backgroundColor",function(index,value){
  4. return index===0 ? "red" : "blue";
  5. });//div的序列号是0的div是红色,其他事蓝色;
  6. $("div").css({
  7. "width":"100px",
  8. height:"100px"
  9. });//设置div宽高为100px;
  10. $("div").css({
  11. "width":function(index){
  12. return (index+1)*20+"px"
  13. },
  14. height:function(index){
  15. return (index+1)*20+"px"
  16. },
  17. backgroundColor:function(index){
  18. return "#"+(Math.floor(Math.random()*256*256*256)).toString(16);
  19. }
  20. });//设置idv依次从小到大的正方形,背景颜色随机;如下图

在这里插入图片描述

  1. 直接添加样式,利用class名在css样式中的样式,这里不需要加.(点);
  2. $("div").addClass("div0 div2");
  3. //当添加多个样式时,用空格分开;(如:添加div0的样式和div2的样式);
  4. $("div").removeClass("div2");
  5. //可以移除其中任意一个样式;
  6. $("div").addClass("div0").on("click",function(e){
  7. console.log(this);//这个的this被点击的是DOM对象
  8. $(this).toggleClass("div1");//切换div1的样式;形成开关的效果;
  9. $(this).toggleClass("div1",true);//切换一次div1样式;
  10. $(this).toggleClass("div1",false);//不可切换样式;
  11. })

六、DOM宽高和位置

  1. $("div").width(100);//设置宽度为100px;
  2. console.log($("div").width());//获取宽度,值为纯数字;
  3. $("div").on("click",function(e){
  4. $(this).width($(this).width()+10);
  5. });//当点击div时,帝女的宽度加10px;
  6. $("div").height(100);//设置高度,同宽度一样;
  7. $("div").height(function(index,value){
  8. return value+10;
  9. });
  10. //仅获取,但是尽量不要设置它的值;
  11. console.log($("div").innerWIdth());//width+padding;
  12. $("div").innerWidth(100);//尽量不要设置;
  13. console.log($("div").outerWidth());//width+padding+border;
  14. console.log($("div").outerWidth(trun));//width+padding+border+margin;
  15. //仅获取,不可设置的属性;
  16. console.log($("div").offset().left);//{left.top}对象;
  17. //let {left,top}=$("div").offset();
  18. //console/log(left,top);
  19. //position相对于父元素定位的位置,offset是相对于页面的位置;
  20. console.log($(".div1").position());
  21. console.log("(.div1").offset());

这些都是jQuery的基础知识, 为了方便理解和甄别.

发表评论

表情:
评论列表 (有 0 条评论,298人围观)

还没有评论,来说两句吧...

相关阅读

    相关 认识name属性

    认识name属性 我们Python函数中,如果一个函数调用其他函数完成一项功能,我们称这个函数为主函数,如果一个函数没有调用其他函数,我们把这种函数叫做非主函数。

    相关 jquery 获取data-* 属性

    `data-`自定义数据属性 > HTML5规定可以为元素添加非标准型的属性,只需添加前缀data-,这些属性可以随意添加,随意命名,目的是为元素提供与渲染无关的信息,或

    相关 CSS-属性选择符

    标签中的属性,可以用来详细的描述元素,当然也可以用来选择元素。 CSS的属性选择符中,属性名是放在一对方括号\[\]中的 如果要使用多个属性,可以连续写多个,例如