javascript - 使用 Jquery 无法在 Div 中加载 html 页面
问题描述
我正在尝试在单击链接时加载 Html 页面,但由于某种原因它根本不起作用。
这是我的 LogIn.HTML 页面:
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#game1").click(function(){
$("#result1").load('Game.html');
});
$("#players").click(function(){
$("#result1").load('players.html');
});
});
</script>
</head>
<body>
<table>
<tr>
<td><a id="game1" href="" style="color:white;">Game</a></td>
</tr>
<tr>
<td><a href="" id="players" style="color:white;">players</a></td>
</tr>
</table>
<div id="result1" >
</div>
</body>
</html>
这是我的基本 Game.html(与 Players.html 相同的概念):
<!DOCTYPE>
<html>
<head>
<h1>this is first game!!!</h1>
</head>
</html>
我已经尝试了很多解决方案,但我无法让它工作,我也使用 chrome(不是本地主机,常规的)打开 html 页面,它会影响吗?
解决方案
您可以尝试将脚本标签移至底部:如this SO中所建议
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<table>
<tr>
<td><a id="game1" href="" style="color:white;">Game</a></td>
</tr>
<tr>
<td><a href="" id="players" style="color:white;">players</a></td>
</tr>
</table>
<div id="result1"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#game1").click(function(){
$("#result1").load('https://full/path/to/Game.html');
});
$("#players").click(function(){
$("#result1").load('https://full/path/to/players.html');
});
});
</script>
</body>
</html>
更新:
加上你错过了
#
in"game1"
,在上面的例子中更正了。更正了
#game1
评论中指出的html
推荐阅读
- python - 如果 df2 中不存在,则从 df1 获取数据
- firebase - 没有 Firebase 的电话 otp 和验证
- java - 在 Java 中加载 UTF-8 编码的 XML 数据时出现数据库错误
- android - 如何在颤动中实现这个应用程序栏设计,在其中我将导航插入应用程序栏而不是底部导航栏
- drupal-8 - Drupal webform 中的字段之一可以是所见即所得的 HTML 字段吗?
- ios - 哪个视频编解码器与 opencv videowriter 一起使用来生成 IOS 或 android 手机可读的视频文件?
- c# - 从文本框到数据网格 WPF C#
- xamarin.forms - 在 Xamarin 表单中检测越狱绕过
- python - ValueError:计算方法分配失败(Python3 odoo)
- javascript - 默认情况下如何在 Material UI 中切换行中的内容?