jQuery 笔记

1. 选择器  http://www.runoob.com/jquery/jquery-selectors.html

 

2. toggle()  用来切换 hide() 和 show() 方法   http://www.runoob.com/try/try.php?filename=tryjquery_toggle

http://www.runoob.com/jquery/jquery-hide-show.html

$(selector).toggle(speed,callback);
可选的 speed 参数规定隐藏/显示的速度,可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是 toggle() 方法完成后所执行的函数名称。

3. 淡入淡出   fadeIn  fadeOut  fadeToggle  fadeTo        http://www.runoob.com/jquery/jquery-fade.html

4. 滑动   slideDown   slideUp  slideToggle     http://www.runoob.com/jquery/jquery-slide.html

5. 动画     http://www.runoob.com/jquery/jquery-animate.html

 $("div").animate({
    left:'250px',
    opacity:'0.5',
    height:'150px',
    width:'150px'
  });
$("div").animate({
    left:'250px',
    height:'+=150px',
    width:'+=150px'
  });
$("div").animate({
    height:'toggle'
  });
6. 设置内容和属性  不止是值 可以是函数   http://www.runoob.com/jquery/jquery-dom-set.html

$("#test1").text("Hello world!");
$("#test2").html("<b>Hello world!</b>");
$("#test3").val("Dolly Duck");
$("#test1").text(function(i,origText){
    return "Old text: " + origText + " New text: Hello world!
    (index: " + i + ")"; 
  });

$("#test2").html(function(i,origText){
    return "Old html: " + origText + " New html: Hello <b>world!</b>
    (index: " + i + ")"; 
  });
attr() 方法也允许您同时设置多个属性

$("#w3s").attr({
    "href" : "http://www.w3cschool.cc/jquery",
    "title" : "W3Schools jQuery Tutorial"
  });
jQuery 方法 attr(),也提供回调函数(不止是值也可以是函数)

$("#w3s").attr("href", function(i,origValue){
    return origValue + "/jquery"; 
  });
7. 添加元素  

  • append() - 在被选元素的结尾插入内容
  • prepend() - 在被选元素的开头插入内容
  • after() - 在被选元素之后插入内容
  • before() - 在被选元素之前插入内容
8. 删除元素  http://www.runoob.com/jquery/jquery-dom-remove.html

  • remove() - 删除被选元素(及其子元素)
  • empty() - 从被选元素中删除子元素
过滤被删除的元素    $("p").remove(".italic");

9. 操作 CSS  http://www.runoob.com/jquery/jquery-css-classes.html

  • addClass() - 向被选元素添加一个或多个类
  • removeClass() - 从被选元素删除一个或多个类
  • toggleClass() - 对被选元素进行添加/删除类的切换操作
  • css() - 设置或返回样式属性
10. 设置多个 CSS 属性    http://www.runoob.com/jquery/jquery-css.html

$("p").css({"background-color":"yellow","font-size":"200%"});
11.  遍历父   http://www.runoob.com/jquery/jquery-traversing-ancestors.html

$("span").parents("ul");//返回所有 <span> 元素的所有祖先,并且它是 <ul> 元素
$("span").parentsUntil("div");//返回介于 <span> 与 <div> 元素之间的所有祖先元素
12. 遍历子孙    http://www.runoob.com/jquery/jquery-traversing-descendants.html
$("div").children("p.1");//返回类名为 "1" 的所有 <p> 元素,并且它们是 <div> 的直接子元素
$("div").find("span");//返回属于 <div> 后代的所有 <span> 元素
$("div").find("*");//返回 <div> 的所有后代
13. 遍历同胞   http://www.runoob.com/jquery/jquery-traversing-siblings.html

  • siblings()
  • next()
  • nextAll()
  • nextUntil()
  • prev()
  • prevAll()
  • prevUntil()
14. 添加过滤的遍历   http://www.runoob.com/jquery/jquery-traversing-filtering.html

$("div p").first();//选取首个 <div> 元素内部的第一个 <p> 元素
$("div p").last();//选择最后一个 <div> 元素中的最后一个 <p> 元素
$("p").eq(1);//选取第二个 <p> 元素(索引号 1)
$("p").filter(".intro");//返回带有类名 "intro" 的所有 <p> 元素
$("p").not(".intro");//返回不带有类名 "intro" 的所有 <p> 元素
15. toArray 

//把 <li> 元素转换为数组,然后输出该数组元素的 innerHTML
$("button").click(function(){
x=$("li").toArray()
for (i=0;i<x.length;i++)
{
alert(x[i].innerHTML);
}
});







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 jQuery 的事件,我可以和你分享一些学习笔记。 1. 绑定事件 在 jQuery 中,我们可以通过以下方式来绑定事件: ``` $(selector).event(function(){ // 事件处理程序 }) ``` 其中,`selector` 表示要绑定事件的元素,`event` 表示要绑定的事件类型,比如 `click`、`mouseover` 等等。事件处理程序则是在事件触发时要执行的代码块。 2. 多个事件绑定 我们可以通过 `on()` 方法来同时绑定多个事件: ``` $(selector).on({ event1: function(){ // 事件处理程序1 }, event2: function(){ // 事件处理程序2 } }) ``` 这样,当 `event1` 或 `event2` 中任意一个事件触发时,对应的处理程序都会被执行。 3. 解除事件 如果需要解除某个元素的事件处理程序,可以使用 `off()` 方法: ``` $(selector).off(event); ``` 其中,`event` 表示要解除的事件类型。如果不指定事件类型,则会解除该元素上所有的事件处理程序。 4. 事件委托 在 jQuery 中,我们可以使用事件委托来提高性能。事件委托是指将事件绑定到父元素上,而不是绑定到子元素上,然后通过事件冒泡来判断是哪个子元素触发了该事件。这样,当子元素数量较多时,只需要绑定一次事件,就可以监听到所有子元素的事件。 ``` $(selector).on(event, childSelector, function(){ // 事件处理程序 }) ``` 其中,`selector` 表示父元素,`event` 表示要绑定的事件类型,`childSelector` 表示要委托的子元素的选择器,事件处理程序则是在子元素触发事件时要执行的代码块。 以上是 jQuery 中事件的一些基本操作,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值