【问题标题】:Eclipse JQuery not working on my simple Hello World programEclipse JQuery 不适用于我的简单 Hello World 程序
【发布时间】:2019-12-11 08:02:51
【问题描述】:

我几天来一直在尝试让 JQuery 处理一个简单的 hello world 程序。我的程序所做的是当我按下一个按钮时,它是否将 XMLHTTPRequest() 发送到 java servlet 并将“Hey World”更改为“Hello World”。我是 JQuery 和 javascript 的新手。我知道您的项目中需要有一个 jquery.js 文件,它目前位于我的 WebContent/WEB-INF/ 文件夹以及我的 JavaScript 资源文件夹中。我正在使用 HTML 文件,唯一使用 JQuery 的行是 $("#here").html(this.responseText),当我将其更改为 document.getElementById("here").innerHTML = this.responseText; 时,程序可以正常工作。虽然这种 JavaScript 方式有效,但我想学习如何使用 JQuery 来做到这一点,但我首先需要让它工作。

我看过类似的问题,但要么不清楚,要么解决方案不起作用。我试过包括 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> 在我的实际脚本之前,还有src="jquery-3.4.1.js"src="WebContent/WEB-INF/jquery-3.4.1.js".

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-3.4.1.js"></script>
<script type="text/javascript">

function change2(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200){
    $("#here").html(this.responseText);
  }
};
var current = document.getElementById("here").innerHTML.toString();
xmlhttp.open("GET", "helloWorld", true);
xmlhttp.send();
}
</script>
</head>
<body>
<h1 id="here">Hey World</h1>
<button type="button" onclick="change2()">Change</button>
</body>
</html>

我真是大吃一惊。

【问题讨论】:

    标签: javascript jquery servlets


    【解决方案1】:

    尝试使用

    $.ajax({url: "Your_URL", success: function(result){
    $("#here").html(result);}});
    

    jQuery 简单易用

    你可以看看这篇文章 https://www.sitepoint.com/use-jquerys-ajax-function/

    【讨论】:

    • 非常感谢您的帮助。这到底应该去哪里@husseiELbadady。抱歉,有点新。
    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多