您的位置:首页 > Web前端 > JQuery

jQuery 入门教程(12): HTML Get

2013-09-27 00:00 483 查看
jQuery库包含了很多用来改变和操作HTML元素及其属性的方法。
其中一个非常重要的部分就是jQuery可以用来操作DOM。
本篇介绍使用jQuery来取得DOM节点元素的值或属性。
其中三个简单而有用的方法如下:

text() – 设置或取得指定元素的文本内容。

html() – 设置或取得指定元素的内容(包括HTML标记)

val() – 设置或取得表单某个输入域的值。

例如,下面代码使用html()和text()方法取得HTML元素的内容:

[javascript]
view plain
copy
print
?

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

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

});

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

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

});

下面的代码取得Form中Input 的内容:

[javascript]
view plain
copy
print
?

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

alert("Value: " + $("#test").val());

});

除了上面的方法外,attr()方法用来取得某个元素的属性:

下面代码用来取得链接的href属性:

[html]
view plain
copy
print
?

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>JQuery Demo</title>

<script src="scripts/jquery-1.9.1.js"></script>

</script>

<script>

$(document).ready(function () {

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

alert($("#guidebee").attr("href"));

});

});

</script>

</head>

<body>

<p><a

href="http://www.imobilebbs.com"

id="guidebee">

imobilebbs.com

</a></p>

<button>Show href Value</button>

</body>

</html>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: