jquery

2018-07-10  本文已影响1人  如沐春风_da89

JQuery常用方法

jQueryHTML

捕获

$("button").click(function(){
  alert($("#runoob").attr("href"));
});

设置

$("#btn1").click(function(){
    $("#test1").text("Hello world!");
});
$("#btn2").click(function(){
    $("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
    $("#test3").val("RUNOOB");
$("button").click(function(){
  $("#runoob").attr("href","http://www.runoob.com/jquery");
});
$("button").click(function(){
    $("#runoob").attr({
        "href" : "http://www.runoob.com/jquery",
        "title" : "jQuery 教程"
    });
});

添加元素

删除元素

css类

css()方法

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

尺寸

jQuery遍历

jQuery祖先

$(document).ready(function(){
  $("span").parent();
});
$(document).ready(function(){
  $("span").parents("ul");
});
$(document).ready(function(){
  $("span").parentsUntil("div");
});

jQuery后代

$(document).ready(function(){
  $("div").children();
});

您也可以使用可选参数来过滤对子元素的搜索。
下面的例子返回类名为 "1" 的所有 <p> 元素,并且它们是 <div> 的直接子元素

$(document).ready(function(){
  $("div").children("p.1");
});
$(document).ready(function(){
  $("div").find("span");
});

jQuery 同胞

next() 方法返回被选元素的下一个同胞元素。
该方法只返回一个元素。
下面的例子返回 <h2> 的下一个同胞元素:

$(document).ready(function(){
  $("h2").next();
});

jQuery 过滤

$(document).ready(function(){
  $("p").eq(1);
});
$(document).ready(function(){
   $("p").filter(".url").css("background-color","yellow");
});

not() 方法与 filter() 相反。

上一篇 下一篇

猜你喜欢

热点阅读