【发布时间】:2015-04-27 22:00:09
【问题描述】:
有几个类似的问题,所以我希望这是一个独特的问题。关于这些类似问题的建议解决方案都没有解决我的问题。如果我搞砸了,请这位初学者谦虚地道歉。
我的页面上有一个空的 div,我正在使用带有数组字符串的 javascript 加载。目前,我有一个在重新加载整个页面的按钮上运行的脚本。我希望该按钮仅使用我的 javascript 数组中的项目重新加载 div。
这是我的代码:
<html>
<head>
<link rel="stylesheet" href="obliqueStyle.css">
<style></style>
</head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<body>
<div id="wrapper">
<div id="strategyBox"></div>
<div id="button">
<a class="againbutton" onclick="buttonReload()">Again</a>
<script>
var buttonReload = function() {
document.getElementById("strategyBox").innerHTML = '<p id="strategyText">' + randomStrategy + '</p>';
}
</script>
</div>
</div>
<script src="os.js"></script>
</body>
这是我的数组和 JS 的 sn-p(来自 index.html 中引用的 os.js 文件)我用来初始/刷新时加载 div:
var obliqueStrategy = ["Abandon normal instruments",
"Accept advice",
"Accretion",
"A line has two sides"];
var randomStrategy = obliqueStrategy[Math.floor(Math.random() * obliqueStrategy.length)];
document.getElementById("strategyBox").innerHTML = '<p id="strategyText">' + randomStrategy + '</p>';
我尝试在 html 中调用与脚本中的函数相同的 javascript,如下所示:
<div id="button">
<a class="againbutton" onclick="buttonReload()">Again</a>
<script>
var buttonReload = function() {
document.getElementById("strategyBox").innerHTML = '<p id="strategyText">' + randomStrategy + '</p>';
}
</script>
</div>
我尝试过像这样使用 jQuery AJAX 加载函数:
<script>
$(function() {
$("#againbutton").on("click", function() {
$("#strategyBox").load("index.html")
return false;
})
})
</script>
我已经尝试了上述的变体并尝试了其他一些我忘记了我究竟是如何以及做了什么的事情,所以我不能包括它们。尽管它看起来非常简单,但我确实遇到了困难。
提前感谢您的帮助。
【问题讨论】:
标签: javascript jquery html ajax