目录javascript
@[基本实例|基于bootstrap框架]css
三个简单实用的用于 DOM 操做的 jQuery 方法:html
$("#btn1").click(function(){ alert("Text: " + $("#test").text()); }); $("#btn2").click(function(){ alert("HTML: " + $("#test").html()); }); $("#btn1").click(function(){ alert("Value: " + $("#test").val()); });
jQuery attr() 方法用于获取属性值。java
$("button").click(function(){ alert($("#w3s").attr("href")); });
咱们将使用前一章中的三个相同的方法来设置内容:jquery
$("#btn1").click(function(){ $("#test1").text("Hello world!"); }); $("#btn2").click(function(){ $("#test2").html("<b>Hello world!</b>"); }); $("#btn3").click(function(){ $("#test3").val("Dolly Duck"); });
jQuery attr() 方法也用于设置/改变属性值。bootstrap
$("button").click(function(){ $("#w3s").attr("href","http://www.w3school.com.cn/jquery"); });
jQuery append() 方法在被选元素的结尾插入内容app
$("p").append("Some appended text.");
jQuery prepend() 方法在被选元素的开头插入内容框架
$("p").prepend("Some prepended text.");
jQuery after() 方法在被选元素以后插入内容。
jQuery before() 方法在被选元素以前插入内容。code
$("img").after("Some text after"); $("img").before("Some text before");
jQuery remove() 方法删除被选元素及其子元素htm
$("#div1").remove();
jQuery empty() 方法删除被选元素的子元素
$("#div1").empty();
- addClass() - 向被选元素添加一个或多个类
$("button").click(function(){ $("h1,h2,p").addClass("blue"); $("div").addClass("important"); });
removeClass() - 从被选元素删除一个或多个类
$("button").click(function(){ $("h1,h2,p").removeClass("blue"); });
toggleClass() - 对被选元素进行添加/删除类的切换操做
$("button").click(function(){ $("h1,h2,p").toggleClass("blue"); });
css() - 设置或返回样式属性