jQuery HTML

alert("Text: " + $("#test").text());获取text

alert("HTML: " + $("#test").html());获取html

alert("Value: " + $("#test").val());获取输入字段的值

获得链接中 href 属性的值

$("#test1").text("Hello world!”);设置文本

$("#test2").html("<h1>Hello world!</h1>”);设置html

$("#test3").val("Dolly Duck”);设置值

text()、html() 以及 val() 的回调函数

上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

实例

<html>

<head>

<script src="/jquery/jquery-1.11.1.min.js"></script>

<script>

$(document).ready(function(){

  $("#btn1").click(function(){

    $("#test1").text(function(i,origText){

      return "Old text: " + origText + " New text: Hello world! (index: " + i + ")"; 

    });

  });

  $("#btn2").click(function(){

    $("#test2").html(function(i,origText){

      return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + i + ")"; 

    });

  });

});

</script>

</head>

<body>

<p id="test1">这是<b>粗体</b>文本。</p>

<p id="test2">这是另一段<b>粗体</b>文本。</p>

<button id="btn1">显示旧/新文本</button>

<button id="btn2">显示旧/新 HTML</button>

</body>

</html>

设置/改变属性值。

 $("#w3s").attr({

    "href" : "http://www.w3school.com.cn/jquery",

    "title" : "W3School jQuery Tutorial"

  });

同时设置多个属性。

$("button").click(function(){

  $("#w3s").attr("href", function(i,origValue){

    return origValue + "/jquery";

  });

});

jQuery 方法 attr(),也提供回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

  

jQ append() 方法在被选元素的结尾插入内容。

jQuery prepend() 方法在被选元素的开头插入内容。

append() 和 prepend() 方法能够通过参数接收无限数量的新元素。可以通过 jQuery 来生成文本/HTML(就像上面的例子那样),或者通过 JavaScript 代码和 DOM 元素。

function appendText()

{

var txt1="<p>Text.</p>";               // 以 HTML 创建新元素

var txt2=$("<p></p>").text("Text.");   // 以 jQuery 创建新元素

var txt3=document.createElement("p");  // 以 DOM 创建新元素

txt3.innerHTML="Text.";

$("p").append(txt1,txt2,txt3);         // 追加新元素

$("img").after(txt1,txt2,txt3);          // 在 img 之后插入新元素

jQuery after() 方法在被选元素之后插入内容。

jQuery before() 方法在被选元素之前插入内容

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

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

过滤被删除的元素

(jQuery remove() 方法也可接受一个参数,允许您对被删元素进行过滤。

该参数可以是任何 jQuery 选择器的语法。

下面的例子删除 class="italic" 的所有 <p> 元素:)

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

向不同的元素添加 class 属性。当然,在添加类时,您也可以选取多个元素

$("button").click(function(){

  $("h1,h2,p").addClass("blue");

  $("div").addClass("important");

});

也可以在 addClass() 方法中规定多个类:

$("button").click(function(){

  $("#div1").addClass("important blue");

});

不同的元素中删除指定的 class 属性:

$("button").click(function(){

  $("h1,h2,p").removeClass("blue");

});

jQuery toggleClass() 方法 : 该方法对被选元素进行添加/删除类的切换操作:

$("button").click(function(){

  $("h1,h2,p").toggleClass("blue");

});

jQuery css() 方法

css() 方法设置或返回被选元素的一个或多个样式属性。

返回指定的 CSS 属性的值

设置指定的 CSS 属性

实例: $("p").css("background-color");

设置多个 CSS 属性

jQuery 尺寸 方法

jQuery 提供多个处理尺寸的重要方法:

  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()

jQuery width() 和 height() 方法

width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。

height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)

实例

$("button").click(function(){

  var txt="";

  txt+="Width: " + $("#div1").width() + "</br>";

  txt+="Height: " + $("#div1").height();

  $("#div1").html(txt);

});

jQuery innerWidth() 和 innerHeight() 方法

innerWidth() 方法返回元素的宽度(包括内边距)。

innerHeight() 方法返回元素的高度(包括内边距)。

下面的例子返回指定的 <div> 元素的 inner-width/height:

实例

$("button").click(function(){

  var txt="";

  txt+="Inner " + $("#div1").innerWidth() + "</br>";

  txt+="Inner height: " + $("#div1").innerHeight();

  $("#div1").html(txt);

});

jQuery outerWidth() 和 outerHeight() 方法

outerWidth() 方法返回元素的宽度(包括内边距和边框)。

outerHeight() 方法返回元素的高度(包括内边距和边框)。

下面的例子返回指定的 <div> 元素的 outer-width/height:

outerWidth(true) 方法返回元素的宽度(包括内边距和边框)。

outerHeight(true) 方法返回元素的高度(包括内边距和边框)。

实例

$("button").click(function(){

  var txt="";

  txt+="Outer " + $("#div1").outerWidth() + "</br>";

  txt+="Outer height: " + $("#div1").outerHeight();

  txt+="Outer width of div: " + $("#div1").outerWidth() + "</br>";

  txt+="Outer height of div: " + $("#div1").outerHeight();

  $("#div1").html(txt);

});

jQuery - 更多的 width() 和 height()

下面的例子返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度:

实例

$("button").click(function(){

  var txt="";

  txt+="Document width/height: " + $(document).width();

  txt+="x" + $(document).height() + " ";

  txt+="Window width/height: " + $(window).width();

  txt+="x" + $(window).height();

  alert(txt);

});

下面的例子设置指定的 <div> 元素的宽度和高度:

实例

$("button").click(function(){

  $("#div1").width(500).height(500);

});

原文地址:https://www.cnblogs.com/zh-0802/p/6029358.html