如何使用 jQuery 獲取所有匹配元素的文字內容?
要使用 jQuery 獲取所有匹配元素的文字內容,請使用 text() 方法。你可以嘗試執行以下程式碼,以使用 jQuery 獲取所有匹配元素的文字內容 -
示例
<html>
<head>
<title>The Selector Example</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#button1").click(function(){
var content = $("#pid2").text();
alert(content);
});
});
</script>
<style>
.red {
color:red;
}
.green {
color:green;
}
</style>
</head>
<body>
<p class = "green" id = "pid1">This is <i>first paragraph</i>.</p>
<p class = "red" id = "pid2">This is second paragraph.</p>
<button id="button1">Get</button>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP