关于JQUERY的小知识点

window.onload = function(){   //原生js设置背景属性

var oBox = document.getElementById('box');

oBox.onclick = function(){

this.style.background = 'yellow';

}

}

$(function(){    //jquery写法

$('#box').click(function(){

$(this).css('background','yellow')

})

})

$(function(){   //混合写法,get()!

$('#box').click(function(){

$(this).get(0).style.background = 'yellow'

})

})

若有li弹出它的长度,方法如下:

alert($('li').size())

alert($('li').length)

设置最后一个元素的背景:

$('li').eq($('li').size()-1).css('background','yellow')

$('li').eq(-1).css('background','yellow')

弹出DIV的宽:

alert($('#box').css('width'))  //conetnt

alert($('#box').outerWidth())  //padding + content

alert($('#box').outerWidth(true)) //padding + margin + content

高:

alert($('#box').css('width'))  //conetnt

alert($('#box').outerheight())  //padding + content

alert($('#box').outerheight(true)) //padding + margin + content

Text 和 HTML 的区别:

Hello ! star I'm sky

alert($('#txt').text()); //弹出Hello ! star I'm sky

alert($('#txt').html()); //弹出Hello ! star I'm sky

$('p').text('Tomorrow'); //添加标签i后Tomorrow

$('p').html('Tomorrow'); //Tomorrow

移除再拉回:

var box = $('#box'); //方法1

$('#box').remove();  //remove可以用detach代替

$('body').append(box);

var box = $('#box').detach(); //方法2 同上

$('body').append(box);

detach 会有一个返回值$('#box'),直接存在变量里

parent的注意事项:

$('#son').parent().css('backgroundColor','green');//获取父级标签

$('#son').parents().css('backgroundColor','green');//父及 及以上同时可传选择器

$('#son').parents('.lala').css('background','black')//父元素中有lala标签的

//最贴近自己的父及,且必须传参,仅能找到最近的父级标签

$('#son').closest('lala').css('background','black')

兄弟节点siblings:

//不传参就是不包括自身,传参就是过滤后找到那个参数

$('.two').siblings().css('background','pink')

$('.two').siblings('.one').css('background','pink')

//next仅能返回一个对象,可以写多个next

$('.two').next().next().next().css('background','pink')

//返回一组对象可以传参,不传参就是下面的全都被选中

$('.two').nextAll('.one').css('background','pink')

//从two到four之间的对象都被选中,不传参下面的都被选中同nextAll不传参一样

$('.two').nextUntil('four').css('background','pink')

//过滤,除了dd其他都改变,not则相反

$('li').css('background','pink').filter('.dd').css('background','yellow')

$('li').css('background','pink').not('.dd').css('background','yellow')

wrap:(只能打包同一种标签)

$('span').wrap('

')//把每个span外面都套了个div

$('span').wrapAll('

')//把所有的span都套在一个div里面

$('span,p').wrapAll('

')//打包所有的标签

$('span').unwrap().unwrap()//拆包

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • jQuery基础 什么是JQ?一个优秀的JS库,大型开发必备JQ的好处?一简化JS的复杂操作二不再需要关心兼容性三...
    幺七阅读 4,489评论 0 2
  • HTML 对html5的理解,web语义话,SEO 页面加载过程 新增API,本地存储,Canvas CSS 经典...
    linwalker阅读 3,843评论 0 3
  • 本文是针对刚学编程的小白,都是一些基础知识,如果想了解更多深层一点的东西,欢迎移步本人博客!! 博客地址 点击跳转...
    西巴撸阅读 3,613评论 0 0
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 5,910评论 0 1
  • Words & Phrases No.1 massacre. to kill a lot of people or...
    Ada_yao阅读 1,513评论 0 0

友情链接更多精彩内容