JS XMLHttpRequest()如何只读取到网页<pre>标签里的内容呢
时间: 2024-06-10 15:07:11 浏览: 13
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();
```
相关问题
JS XMLHttpRequest()如何只读取到网页源代码里<pre>标签和</pre>标签中间的内容呢
可以使用正则表达式来提取<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循环来提取所有内容。
<?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>
<script src="script.js"></script>
</head>
<body>
<h1>Interactive Bookstore</h1>
<label for="books">Select a book:</label>
<select id="books" onchange="displayBookInfo()">
<option value="">--Select a book--</option>
</select>
<br><br>
<table id="bookInfoTable">
<tr>
<th>Category</th>
<th>Author(s)</th>
<th>Year</th>
<th>Price</th>
</tr>
</table>
</body>
</html>
```
JavaScript文件:
```javascript
// 定义一个变量来保存XML文件
var xmlDoc;
// 当页面加载时读取XML文件
window.onload = function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
xmlDoc = this.responseXML;
populateBookList();
}
};
xmlhttp.open("GET", "books.xml", true);
xmlhttp.send();
}
// 填充下拉单的选项
function populateBookList() {
var selectNode = document.getElementById("books");
// 获取所有书本的节点
var bookNodes = xmlDoc.getElementsByTagName("book");
// 遍历所有书本的节点,并将它们添加到下拉单选项中
for (var i = 0; i < bookNodes.length; i++) {
var optionNode = document.createElement("option");
optionNode.textContent = bookNodes[i].getElementsByTagName("author")[0].textContent + " - " + bookNodes[i].getElementsByTagName("year")[0].textContent;
optionNode.value = i;
selectNode.appendChild(optionNode);
}
}
// 显示选中书本的信息
function displayBookInfo() {
var selectNode = document.getElementById("books");
var index = selectNode.value;
var bookNodes = xmlDoc.getElementsByTagName("book");
var bookNode = bookNodes[index];
// 清空表格内容
var tableNode = document.getElementById("bookInfoTable");
tableNode.innerHTML = "";
tableNode.appendChild(createTableRow("Category", bookNode.getAttribute("category")));
tableNode.appendChild(createTableRow("Author(s)", getAuthors(bookNode)));
tableNode.appendChild(createTableRow("Year", bookNode.getElementsByTagName("year")[0].textContent));
tableNode.appendChild(createTableRow("Price", bookNode.getElementsByTagName("price")[0].textContent));
}
// 获取书本的所有作者
function getAuthors(bookNode) {
var authorNodes = bookNode.getElementsByTagName("author");
var authorList = "";
for (var i = 0; i < authorNodes.length; i++) {
if (i > 0) {
authorList += ", ";
}
authorList += authorNodes[i].textContent;
}
return authorList;
}
// 创建一个表格行
function createTableRow(label, value) {
var trNode = document.createElement("tr");
var tdLabelNode = document.createElement("td");
tdLabelNode.textContent = label;
var tdValueNode = document.createElement("td");
tdValueNode.textContent = value;
trNode.appendChild(tdLabelNode);
trNode.appendChild(tdValueNode);
return trNode;
}
```
注意,这个例子假设XML文件名为“books.xml”,需要放在同一目录下。可以根据需要进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
<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)