我尝试收到提示信息。但它没有用。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="WebApplication9.WebForm10" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1 id="main-heading">Hello world!</h1>
<script>
$(document).ready(function () {
var headingElement = $("#main-heading");
console.log(headingElement.innerHTML);
var newHeadingText = prompt("Please provide a new heading:");
headingElement.innerHTML = newHeadingText;
});
</script>
</div>
</form>
</body>
</html>
所以这就是Jquery的方式。但提示不会显示。
如何改变这一点,提示会显示?
谢谢
但你说:
在标记之前,如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="WebApplication9.WebForm10" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1 id="main-heading">Hello world!</h1>
</div>
</form>
<script>
$(document).ready(function () {
var headingElement = $("#main-heading");
console.log(headingElement.text);
var newHeadingText = prompt("Please provide a new heading:");
headingElement.innerHTML = newHeadingText;
});
</script>
</body>
</html>
但这不起作用。
哦,我现在就这样:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="WebApplication9.WebForm10" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1 id="main-heading">Hello world!</h1>
</div>
</form>
<script>
$(document).ready(function () {
var headingElement = $("#main-heading");
console.log(headingElement.text());
var newHeadingText = prompt("Please provide a new heading:");
headingElement.text(newHeadingText);
});
</script>
</body>
</html>
但现在仍然显示提示。
答案 0 :(得分:5)
应在正文结束标记</body>
之前添加脚本。
$(document).ready(function () {
var headingElement = $("#main-heading");
console.log(headingElement.text());
var newHeadingText = prompt("Please provide a new heading:");
headingElement.text(newHeadingText);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" runat="server">
<div>
<h1 id="main-heading">Hello world!</h1>
</div>
</form>
此外,您应该使用text
方法。有关此方法的信息,请查看here。在jquery对象上没有任何名为innerHTML
的属性(headingElement
是一个jquery对象,它与document.getElementById('main-heading')
不同,它会返回一个Element对象,它有一个名为innerHTML
)的属性。