jquery中遍历.text()内容
时间: 2024-05-11 17:15:20 浏览: 13
要遍历jQuery对象中所有元素的文本内容,可以使用`.each()`方法,然后使用`.text()`方法获取每个元素的文本内容,例如:
```javascript
$('p').each(function() {
var text = $(this).text();
console.log(text);
});
```
这将遍历所有`<p>`元素,将每个元素的文本内容打印到控制台。
相关问题
jquery中遍历.text()遍历文本内容
jQuery中的.text()方法可以用于获取或设置元素的文本内容。当使用该方法时,如果不传递任何参数,则返回当前元素的文本内容。如果传递一个参数,则将该参数设置为元素的文本内容。
例如,以下代码将遍历所有p元素,并将它们的文本内容输出到控制台中:
```
$("p").each(function(){
console.log($(this).text());
});
```
在上面的代码中,使用了jQuery的.each()方法遍历所有p元素,并使用.text()方法获取每个元素的文本内容。然后将文本内容输出到控制台中。
jQuery 遍历表格
可以使用 jQuery 的遍历方法来遍历表格。下面是一个使用 jQuery 遍历表格的示例代码:
```html
<table>
<tr>
<th>名称</th>
<th>价格</th>
</tr>
<tr>
<td>商品1</td>
<td>$100</td>
</tr>
<tr>
<td>商品2</td>
<td>$200</td>
</tr>
</table>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function() {
$('table tr').each(function() {
var name = $(this).find('td:first').text();
var price = $(this).find('td:last').text();
console.log(name + ': ' + price);
});
});
</script>
```
在这个示例中,`$('table tr').each()` 方法用于遍历表格的每一行,`$(this).find('td:first')` 和 `$(this).find('td:last')` 方法用于获取当前行的第一个和最后一个单元格的内容。最后将获取到的内容输出到控制台中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)