jQuery基本整理2

jQuery基本整理<2>

@[基本实例|基于bootstrap框架]css


jQuery HTML

jQuery获取

三个简单实用的用于 DOM 操做的 jQuery 方法:html

  • text() - 设置或返回所选元素的文本内容
  • html() - 设置或返回所选元素的内容(包括 - HTML 标记)
  • val() - 设置或返回表单字段的值
$("#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设置

咱们将使用前一章中的三个相同的方法来设置内容:jquery

  • text() - 设置或返回所选元素的文本内容
  • html() - 设置或返回所选元素的内容(包括 HTML 标记)
  • val() - 设置或返回表单字段的值
$("#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添加元素

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删除新元素

jQuery remove() 方法删除被选元素及其子元素htm

$("#div1").remove();

jQuery empty() 方法删除被选元素的子元素

$("#div1").empty();

jQuery CSS

  • 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() - 设置或返回样式属性

相关文章
相关标签/搜索