JS XMLHttpRequest()如何只读取到网页源代码里<pre>标签和</pre>标签中间的内容呢
时间: 2024-06-10 18:07:11 浏览: 12
可以使用正则表达式来提取<pre>标签和</pre>标签中间的内容。具体实现代码如下:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var preContent = xhr.responseText.match(/<pre>(.*?)<\/pre>/gs)[0];
console.log(preContent);
}
};
xhr.send();
这个代码可以实现异步读取指定url的网页源代码,然后使用正则表达式提取其中的<pre>标签和</pre>标签中间的内容。 请注意,这个代码只能读取到第一个出现的<pre>标签和</pre>标签中间的内容。如果有多个这样的标签,可以使用for循环来提取所有内容。
相关问题
JS XMLHttpRequest()如何只读取到网页<pre>标签里的内容呢
JS XMLHttpRequest()可以使用responseText属性获取HTTP响应的内容。你可以先使用getElementsByTagName()方法获取到<pre>标签的元素,再通过innerText属性获取到<pre>标签里的内容。以下是示例代码:
```javascript
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var preTags = document.getElementsByTagName('pre');
if (preTags.length > 0) {
var content = preTags[0].innerText;
console.log(content);
} else {
console.log('No <pre> tags found!');
}
}
};
xhr.open("GET", "http://example.com", true);
xhr.send();
```
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="children"> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="cooking"> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web"> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> <book category="web"> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> </bookstore> 按照上述xml文件,使用html和javascript读取.xml文件的节点内容,以表格方式对内容进行显示,通过下拉单进行交互式书目内容选取,仅显示选中的书目信息
以下是实现该功能的示例代码:
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<title>Interactive Bookstore</title>
</head>
<body>
<select id="category">
<option value="">Select a category</option>
<option value="children">Children</option>
<option value="cooking">Cooking</option>
<option value="web">Web</option>
</select>
<table id="bookTable">
<thead>
<tr>
<th>Author</th>
<th>Year</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script src="script.js"></script>
</body>
</html>
```
JavaScript部分:
```javascript
// 加载xml文件
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
displayBooks(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
// 处理选择器变化事件
document.getElementById("category").addEventListener("change", function() {
var selectedCategory = this.value;
var books = document.getElementsByTagName("book");
// 遍历所有书籍,根据类别显示或隐藏
for (var i = 0; i < books.length; i++) {
if (selectedCategory == "" || books[i].getAttribute("category") == selectedCategory) {
books[i].style.display = "table-row";
} else {
books[i].style.display = "none";
}
}
});
// 显示所有书籍
function displayBooks(xml) {
var xmlDoc = xml.responseXML;
var books = xmlDoc.getElementsByTagName("book");
var bookTable = document.getElementById("bookTable");
// 遍历所有书籍,并在表格中添加新行
for (var i = 0; i < books.length; i++) {
var row = bookTable.insertRow(-1);
row.setAttribute("category", books[i].getAttribute("category"));
row.style.display = "table-row";
var authorCell = row.insertCell(0);
var yearCell = row.insertCell(1);
var priceCell = row.insertCell(2);
authorCell.innerHTML = books[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;
yearCell.innerHTML = books[i].getElementsByTagName("year")[0].childNodes[0].nodeValue;
priceCell.innerHTML = books[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
}
}
```
在该示例中,我们首先使用XMLHttpRequest对象加载books.xml文件。一旦文件加载完成,我们调用displayBooks函数来解析XML并在表格中显示所有书籍信息。
我们还在页面上添加了一个下拉单,使用户可以选择要显示的书籍类别。在选择器变化事件中,我们遍历所有书籍,并根据类别显示或隐藏它们。
在表格中显示书籍信息时,我们使用insertRow函数向表格添加新行,并使用insertCell函数向行添加新单元格。我们还使用getAttribute和getElementsByTagName函数从XML中获取书籍的类别、作者、年份和价格信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<body> <form action="QueryServlet" method="post"> 请输入用户名:<input type="text" name="userName"/> <input type="submit" value="查询"/> 显示全部 </form> 用户名 密码 积分 注册时间 <c:forEach items="${users }" var="item"> ${item.userName } ${item.password } ${item.integral } ${item.registerTime } 删除 </c:forEach> </body>tomcat 删除一条记录怎么写,不用web.xml,详细点
![](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)