要实现Ajax无需按钮或页面刷新即可更新文本区域的功能,可以使用以下解决方法:
使用JavaScript中的XMLHttpRequest对象:
function updateText() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("textArea").innerHTML = xhr.responseText;
}
};
xhr.open("GET", "updateText.php", true);
xhr.send();
}
使用jQuery的Ajax方法:
function updateText() {
$.ajax({
url: "updateText.php",
type: "GET",
success: function(response) {
$("#textArea").html(response);
}
});
}
使用Fetch API:
function updateText() {
fetch("updateText.php")
.then(response => response.text())
.then(data => {
document.getElementById("textArea").innerHTML = data;
});
}
在上述示例中,假设存在一个id为"textArea"的文本区域,通过Ajax请求更新文本内容的URL为"updateText.php"。在服务器端,可以根据具体需求返回所需的文本内容。
下一篇:AJAX下拉菜单级联