day38 jQuery效果与事件

jQuery css() 方法

css() 方法设置或返回被选元素的一个或多个样式属性。
返回 CSS 属性
如需返回指定的 CSS 属性的值,请使用如下语法:

css("propertyname");
$("p").css("background-color");

设置CSS属性
如需设置指定的 CSS 属性,请使用如下语法:

css("propertyname","value");
$("p").css("background-color","yellow");

设置多个CSS属性
如需设置多个 CSS 属性,请使用如下语法:

css({"propertyname":"value","propertyname":"value",...});
$("p").css({"background-color":"yellow","font-size":"200%"});

jQuery css类

addClass()
向被选元素添加一个或多个类

$("h1,p").addClass("blue");
 	$("div").addClass("important"); 
});

removeClass()
从被选元素删除一个或多个类

$("button").click(function(){ $("h1,h2,p").removeClass("blue"); });

toggleClass()
对被选元素进行添加/删除类的切换操作

$("button").click(function(){ $("h1,h2,p").toggleClass("blue"); });

eq()
方法返回带有被选元素的指定索引号的元素,索引号从 0 开头,所以第一个元素的索引号是 0(不是 1)。

$(selector).eq(index)

index()
index() 方法返回指定元素相对于其他同级元素的 index 位置。

$("li").click(function(){
  alert($(this).index());
});

not()
Not() 方法返回指定元素之外的元素。

$("input").not(".in1")

jQuery动画

jQuery隐藏显示
可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素。

$("#hide").click(function(){ 
$("p").hide(); 
}); 
$("#show").click(function(){ 
$("p").show(); 
});

可以使用 toggle() 方法来切换 hide() 和 show() 方法。

$("button").click(function(){ $("p").toggle(); });

jQuery淡入淡出
fadeIn() 用于淡入已隐藏的元素, fadeOut() 方法用于淡出可见元素。

$("button").click(function(){
  $("#div1").fadeIn(); 
$("#div2").fadeIn("slow"); 
$("#div3").fadeOut(3000); 
});

fadeToggle() 方法可以在 fadeIn() 与 fadeOut() 方法之间进行切换。

$("button").click(function(){ ("#div2").fadeToggle("fast");});

fadeTo() 方法允许渐变为给定的不透明度(值介于 0 与 1 之间)。

$("button").click(function(){ $("#div1").fadeTo("slow",0.15);});

jQuery滑动
slideDown() 方法用于向下滑动元素, slideUp() 方法用于向上滑动元素。

$("#flip").click(function(){ $("#div1").slideDown(); $("#div1").slideUp();});

slideToggle() 方法可以在 slideDown() 与 slideUp() 方法之间进行切换。

$("#flip").click(function(){ $("#panel").slideToggle(); });

jQuery自定义动画
animate() 方法用于创建自定义动画。可选的 speed 参数规定效果的时长。它可以取以下值:“slow”、“fast” 或毫秒。可选的 callback 参数是动画完成后所执行的函数名称。

$("button").click(function(){ 
$("div").animate({ 
left:'250px', opacity:'0.5', height:'150px', width:'150px'
 }); });

stop方法
jQuery stop() 方法用于停止动画或效果,在它们完成之前,适用于所有 jQuery 效果函数,包括滑动、淡入淡出和自定义动画。

$("#stop").click(function(){ $("#panel").stop(); });

回调函数
在当前动画 100% 完成之后执行。

$("button").click(function(){ 
$("p").hide("slow",function(){ 
alert("段落现在被隐藏了"); 
}); });

jQuery 事件机制

注册事件
bind() 、on() 方法向被选元素添加一个或多个事件处理程序,以及当事件发生时运行的函数。

$("p").bind("click",function(){
         alert("这个段落被点击了。");
}"dbClick",function(){
         alert("这个段落被双击了。");
});
 $("p").on("click",function(){ alert("段落被点击了。"); }); 

委托事件
delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数

$("div").delegate("p","click",function(){
    $("p").css("background-color","pink");
});

事件对象event
event对象有以下属性
type:事件类型,比如click。
which:触发该事件的鼠标按钮或键盘的键。
target:事件发生的初始对象。
data:传入事件对象的数据。
pageX:事件发生时,鼠标位置的水平坐标(相对于页面左上角)。
pageY:事件发生时,鼠标位置的垂直坐标(相对于页面左上角

$("button").click(function(event){ 
          console.log(evet);
 	});

each()方法
each() 方法为每个匹配元素规定要运行的函数。

$("button").click(function(){ 
$("li").each(function(){ 
alert($(this).text()) 
});  
});

jQuery 对HTML的设置与捕获

jQuery 中非常重要的部分,就是操作 DOM 的能力。
jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易。
html()
html() - 设置或返回所选元素的内容(包括 HTML 标记)。

$("#btn2").click(function(){
 alert("HTML: " + $("#test").html()); 
});
$("#btn2").click(function(){ 
$("#test2").html("<b>Hello world!</b>"); 
});

text()
text() - 设置或返回所选元素的文本内容

$("#btn1").click(function(){ 
alert("Text: " + $("#test").text()); 
});
$("#btn1").click(function(){ 
$("#test1").text("Hello world!"); 
});

val()
val() - 设置或返回表单字段的值

$("#btn1").click(function(){ 
alert("值为: " + $("#test").val()); 
});
$("#btn3").click(function(){ 
$("#test3").val("RUNOOB"); 
});

text()、html() 以及 val() 的回调函数
上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

$("#btn1").click(function(){ 
$("#test1").text(function(i,origText){ 
return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";
}); 
});

attr()、prop()
attr() 、prop()方法用于获取和返回属性值。

$("button").click(function(){ alert($("#runoob").attr("href")); });
$("button").click(function(){ $("#runoob").attr("href","http://www.runoob.com/jquery"); });

具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr();.attr不仅可以返回(设置)元素的原生属性,还可以返回(设置)自定义属性。

jQuery 对HTML的页面尺寸操作

width() 和 height() 方法
width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。
height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。

$("button").click(function(){
"div 的宽度是: " + $("#div1").width() + "</br>";
    "div 的高度是: " + $("#div1").height(20); 
});

innerWidth() 和 innerHeight() 方法
innerWidth() 方法返回元素的宽度(包括内边距)。
innerHeight() 方法返回元素的高度(包括内边距)。

$("button").click(function(){
"div 宽度,包含内边距: " + $("#div1").innerWidth();
"div 高度,包含内边距: " + $("#div1").innerHeight(); 
});

outerWidth() 和 outerHeight() 方法
outerWidth() 方法返回元素的宽度(包括内边距和边框)。
outerHeight() 方法返回元素的高度(包括内边距和边框)。

$("button").click(function(){ 
txt+="div 宽度,包含内边距和边框: " + $("#div1").outerWidth() 
 		txt+="div 高度,包含内边距和边框: " + $("#div1").outerHeight();
 	});

scrollTop() 和 scrollLeft() 方法
scrollTop() 方法设置或者返回滚动条被卷去的元素的高度
scrollLeft() 方法设置或者返回滚动条被卷去的元素的宽度

$("button").click(function(){ alert($("div").scrollTop()); });

jQuery添加元素和删除元素

append()方法
append() 方法在被选元素的结尾插入内容(仍然在该元素的内部)

$("ol").append("<li>追加列表项</li>");

prepend() 方法
prepend() 方法在被选元素的开头插入内容。

$("ol").prepend("<li>追加列表项</li>");

after() 和 before() 方法
jQuery after() 方法在被选元素之后插入内容。
jQuery before() 方法在被选元素之前插入内容。

$("img").before("<b>之前</b>");
$("img").after("<i>之后</i>");

删除元素/内容
remove() - 删除被选元素(及其子元素)
empty() - 从被选元素中删除子元素
empty()仅仅删除元素的文本,占有位置,不显示在页面,但是DOM节点并没有删除
remove()把整个dom节点删除掉,,不占有位置

jquery.color.js的使用

颜色插件

$("button").on("click", function () {
                $("div").animate({"width":200,"background-color":"red"},2000, 					function () {
                    	alert("动画结束");
                });
            });
jquery.lazyload.js的使用

懒加载插件

<img alt="" width="640" height="480" data-original="img/test.jpg" />
$(function() {
		$("img").lazyload();
	});
jquery.ui.js的使用

ui插件
日期选择器

<input type="text" name="date" id="date" />
	$( "#date" ).datepicker();

拖动

  $(function() {
    $( "#draggable" ).draggable();
  });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值