溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jquery取值的方式是什么

發(fā)布時間:2022-05-17 14:55:54 來源:億速云 閱讀:161 作者:iii 欄目:web開發(fā)

這篇文章主要講解了“jquery取值的方式是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“jquery取值的方式是什么”吧!

5種取值方式:1、用“元素對象.text()”獲取指定元素的文本內(nèi)容;2、用“元素對象.html()”獲取元素中包含標簽的內(nèi)容;3、用“輸入框元素.val()”獲取用戶輸入內(nèi)容;4、用“元素對象.attr("屬性名")”獲取指定屬性的值等。

jquery取值的方式是什么

本教程操作環(huán)境:windows7系統(tǒng)、jquery1.10.2版本、Dell G3電腦。

jquery中取值方式有多種方式,下面給大家介紹一下。

1、獲取元素的內(nèi)容值的兩種方法:text()或html()

  • text() 方法可以返回被選元素的文本內(nèi)容。

  • html() 方法可以返回被選元素的內(nèi)容(innerHTML)。

示例:使用text()獲取文本內(nèi)容,使用html()獲取包含標簽的內(nèi)容

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					console.log($("p").text());
					console.log($("p").html());
				});
			});
		</script>
	</head>
	<body>
		<button>獲取p元素的內(nèi)容</button>
		<p>這是一個<b>段落</b>。</p>
	</body>
</html>

jquery取值的方式是什么

2、獲取input輸入框的輸入值

input輸入框的輸入值由value 屬性控制??梢灾苯永胿al() 方法來獲取,也可以attr("value") 方法來獲取。

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					console.log($("input[type=text]").val());
					console.log($("input[type=password]").attr("value"));
				});
			});
		</script>
	</head>
	<body>

		<button>獲取input元素的內(nèi)容</button>
		<p>用戶名: <input type="text" name="user" value="李華" /></p>
		<p>密 碼: <input type="password" name="password" value="123456" /></p>
	</body>
</html>

jquery取值的方式是什么

3、獲取元素屬性值

元素屬性值可以通過兩種方法獲取:

  • attr() 方法

  • prop() 方法

區(qū)別:具有 true 和 false 這兩種取值的布爾屬性,如 checked、selected 和 disabled 等,建議使用 prop() 方法來操作,而其他的屬性都建議使用 attr() 方法來操作。

示例1:使用attr()獲取普通屬性的值

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					console.log($("input").attr("name"));
				});
			});
		</script>
	</head>
	<body>

		<button>獲取name屬性值</button>
		<p>用戶名: <input type="text" name="user" value="李華" /></p>
	</body>
</html>

jquery取值的方式是什么

示例2:使用prop()獲取布爾屬性的值

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					console.log($("input").prop("disabled"));
				});
			});
		</script>
	</head>
	<body>

		<button>獲取disabled屬性值</button>
		<p>密 碼: <input type="password" name="password" value="123456" disabled /></p>
	</body>
</html>

jquery取值的方式是什么

感謝各位的閱讀,以上就是“jquery取值的方式是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對jquery取值的方式是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI