jQuery学习笔记 —— 2. DOM操作和 3. 事件绑定

2、操作DOM元素

  • $("a").attr("href","www.bing.com") 修改属性

  • $("div").html() 获取或修改HTML内容

  • $("div").text() 获取或修改文本内容

  • $("div").addClass() 添加类名

  • $("div").css() 设置样式

  • $("div").removeClass() 移除类名

  • $("div").removeAttr() 移除样式

  • $("div").append() 追加文本或HTML

  • $("div").appendTo() 将已有元素移入

  • $("div").before() 在元素前插入

  • $("div").after() 在元素后插入

  • $("div").clone() 复制一个元素(深复制)

  • $(selector).replaceWith(content) 替换元素

  • $(content).replaceAll(selector) 替换元素

  • $("div").wrap() 包裹元素

  • $("div").wrapInner() 包裹元素内容

  • $(selector).each(function(index))遍历元素

$("span").each(function (index) {
                if (index == 1) {
                    $(this).attr("class", "red");
                }
            });
  • $("div").remove() 删除元素
  • $("div").empty() 清空元素

3、事件

  • ready()事件,两者等价:

    • $(document).ready(function(){})
    • $(function(){})
  • $("li").bind("click mouseon","function")绑定事件

  • $("li").hover("over","out") 类似css中的:hover

  • $("li").toggle(fun1(),fun2(),fun3())绑定多个函数,依次执行

  • $("li").unbind("click",fun) 移除事件,fun为可选参数

  • $("li").one("click",[data],fun) 绑定一次性事件

  • $("li").trigger() 绑定自动事件?

  • $("input").bind("focus|blur",fun) 文本框焦点和失焦

  • $("select").bind("changed",fun) 下拉框改变

  • $("li").live(event,[data],fun) 方法已不建议

posted on 2016-04-08 10:22  网中的小鱼  阅读(167)  评论(0)    收藏  举报

导航