Javascript 为什么jquery不能在ajax函数中工作?
为什么jquery不能在ajax方法中工作Javascript 为什么jquery不能在ajax函数中工作?,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,为什么jquery不能在ajax方法中工作 <body> <h2>The XMLHttpRequest Object</h2> <p id="demo">Let AJAX change this text.</p> <button type="button" onclick="loadDoc()">Change Content</button> <script> function loadDo
<body>
<h2>The XMLHttpRequest Object</h2>
<p id="demo">Let AJAX change this text.</p>
<button type="button" onclick="loadDoc()">Change Content</button>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
$('#demo').html('Hello World');
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
它不起作用了
但这是真的
document.getElementById("demo").innerHTML = 'asas';
为什么??原因何在。我已尝试将id更改为class,但没有成功。尝试添加jQuery
就在标签前面
正如@31piy在他的评论中提到的,您可以像这样使用jQuery的ajax函数:
<script>
$(function() {
$(document).on('click', 'button', function (event) {
event.preventDefault();
$.get('ajax_info.txt', '', function(response, textStatus) {
$('#demo').html('Hello World');
});
});
});
</script>
页面中包含jQuery脚本了吗?奇怪的是,尽管页面中包含了jQuery,但仍然使用原始XHR。如果有jQuery并且Ajax调用成功,那么代码应该可以工作。控制台中的错误消息是什么?它在ajax之外工作吗?您没有提供太多调试信息,也没有提到errors@m.nachuryhtml就是这样做的,演示元素在操作代码中清楚地显示出来
<script>
$(function() {
$(document).on('click', 'button', function (event) {
event.preventDefault();
$.get('ajax_info.txt', '', function(response, textStatus) {
$('#demo').html('Hello World');
});
});
});
</script>