首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

前端框架之jQuery

一 iQuery是什么

jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team

jQuery是继prototype之后又一个优秀的Javascript框架。其宗旨是——WRITE LESS,DO MORE!

它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器

jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTMLdocuments、events、实现动画效果,并且方便地为网站提供AJAX交互。

jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。

二 什么是jQuery对象?

jQuery 对象就是通过jQuery包装DOM对象后产生的对象。jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuery 里的方法: $(“#test”).html();

$("#test").html()

//意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法

// 这段代码等同于用DOM实现代码: document.getElementById(" test ").innerHTML;

//虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DOM对象也不能使用jQuery里的方法.乱使用会报错

//约定:如果获取的是 jQuery 对象, 那么要在变量前面加上$.

var $variable = jQuery 对象

var variable = DOM 对象

$variable[0]:jquery对象转为dom对象 $("#msg").html(); $("#msg")[0].innerHTML

jquery的基础语法:$(selector).action()

使用时到官网下载保存(打开后按Ctrl+s可以保存),放到项目路径下引入

三 寻找元素(选择器和筛选器)

3.1 选择器

3.1.1 基本选择器

$("*") $("#id") $(".class") $("element") $(".class,p,div")

3.1.2 层级选择器

$(".outer div") $(".outer>div") $(".outer+div") $(".outer~div")

3.1.3 基本筛选器

$("li:first") $("li:eq(2)") $("li:even") $("li:gt(1)")

3.1.4 属性选择器

$('[id="div1"]')

3.1.5 表单选择器

$("[type='text']")----->$(":text") 注意只适用于input标签 : $("input:checked")

实例之左侧菜单

left_menu

.menu{

height: 500px;

width: 30%;

background-color: gainsboro;

float: left;

}

.content{

height: 500px;

width: 70%;

background-color: rebeccapurple;

float: left;

}

.title{

line-height: 50px;

background-color: #425a66;

color: forestgreen;}

.hide{

display: none;

}

菜单一

111

111

111

菜单二

111

111

111

菜单三

111

111

111

$(".item .title").click(function () {

$(this).next().removeClass("hide").parent().siblings().children(".con").addClass("hide");

// $(this).next().removeClass("hide");

// $(this).parent().siblings().children(".con").addClass("hide");

})

实例之tab切换

tab

function tab(self){

var index=$(self).attr("xxx");

$("#"+index).removeClass("hide").siblings().addClass("hide");

$(self).addClass("current").siblings().removeClass("current");

}

*{

margin: 0px;

padding: 0px;

}

.tab_outer{

margin: 0px auto;

width: 60%;

}

.menu{

background-color: #cccccc;

/*border: 1px solid red;*/

line-height: 40px;

}

.menu li{

display: inline-block;

}

.menu a{

border-right: 1px solid red;

padding: 11px;

}

.content{

background-color: tan;

border: 1px solid green;

height: 300px;

}

.hide{

display: none;

}

.current{

background-color: darkgray;

color: yellow;

border-top: solid 2px rebeccapurple;

}

菜单一

菜单二

菜单三

内容一

内容二

内容三

3.2 筛选器

3.2.1 过滤筛选器

$("li").eq(2) $("li").first() $("ul li").hasclass("test")

3.2.2 查找筛选器

$("div").children(".test") $("div").find(".test")

$(".test").next() $(".test").nextAll() $(".test").nextUntil()

$("div").prev() $("div").prevAll() $("div").prevUntil()

$(".test").parent() $(".test").parents() $(".test").parentUntil()

$("div").siblings()

四 操作元素(属性,css,文档处理)

4.1 属性操作

--------------------------属性

$("").attr();

$("").removeAttr();

$("").prop();

$("").removeProp();

--------------------------CSS类

$("").addClass(class|fn)

$("").removeClass([class|fn])

--------------------------HTML代码/文本/值

$("").html([val|fn])

$("").text([val|fn])

$("").val([val|fn|arr])

---------------------------

$("").css("color","red")

jQuery循环的两种方式

jquery循环的两种方式

方式一

li=[10,20,30,40]

dic=

$.each(li,function(i,x){

console.log(i,x)

})

方式二

$("tr").each(function(){

console.log($(this).html())

})

实例之全反选

Title

function selectall(){

$("table :checkbox").prop("checked",true)

}

function cancel(){

$("table :checkbox").prop("checked",false)

}

function reverse(){

$("table :checkbox").each(function(){

$(this).prop("checked",!$(this).prop("checked"));

});

}

全选

取消

反选

111

222

333

444

实例之模态对话框

Title

.back{

background-color: rebeccapurple;

height: 2000px;

}

.shade{

position: fixed;

top: 0;

bottom: 0;

left:0;

right: 0;

background-color: coral;

opacity: 0.4;

}

.hide{

display: none;

}

.models{

position: fixed;

top: 50%;

left: 50%;

margin-left: -100px;

margin-top: -100px;

height: 200px;

width: 200px;

background-color: gold;

}

function action1(self){

$(self).parent().siblings().removeClass("hide");

}

function action2(self){

//$(self).parent().parent().children(".models,.shade").addClass("hide")

$(self).parent().addClass("hide").prev().addClass("hide")

}

4.2 文档处理

//创建一个标签对象

//内部插入

$("").append(content|fn) ----->$("p").append("Hello");

$("").appendTo(content) ----->$("p").appendTo("div");

$("").prepend(content|fn) ----->$("p").prepend("Hello");

$("").prependTo(content) ----->$("p").prependTo("#foo");

//外部插入

$("").after(content|fn) ----->$("p").after("Hello");

$("").before(content|fn) ----->$("p").before("Hello");

$("").insertAfter(content) ----->$("p").insertAfter("#foo");

$("").insertBefore(content) ----->$("p").insertBefore("#foo");

//替换

$("").replaceWith(content|fn) ----->$("p").replaceWith("Paragraph. ");

//删除

$("").empty()

$("").remove([expr])

//复制

$("").clone([Even[,deepEven]])

4.3 css操作

CSS

$("").css(name|pro|[,val|fn])

位置

$("").offset([coordinates])

$("").position()

$("").scrollTop([val])

$("").scrollLeft([val])

尺寸

$("").height([val|fn])

$("").width([val|fn])

$("").innerHeight()

$("").innerWidth()

$("").outerHeight([soptions])

$("").outerWidth([options])

实例返回顶部

Title

window.onscroll=function(){

var current=$(window).scrollTop();

console.log(current)

if (current>100){

$(".returnTop").removeClass("hide")

}

else {

$(".returnTop").addClass("hide")

}

}

function returnTop(){

// $(".div1").scrollTop(0);

$(window).scrollTop(0)

}

body{

margin: 0px;

}

.returnTop{

height: 60px;

width: 100px;

background-color: darkgray;

position: fixed;

right: 0;

bottom: 0;

color: greenyellow;

line-height: 60px;

text-align: center;

}

.div1{

background-color: orchid;

font-size: 5px;

overflow: auto;

width: 500px;

}

.div2{

background-color: darkcyan;

}

.div3{

background-color: aqua;

}

.div{

height: 300px;

}

.hide{

display: none;

}

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

返回顶部

识别图中二维码,欢迎关注python宝典

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180701A0BHRA00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券