我正在尝试让shuffle函数在页面加载时发生。到目前为止,只成功地打破了它。
我试过了
$(window).bind("load",function(){});但似乎不能让它做我需要它做的事情。
下面是一个Codepen,它有一个工作样机
任何帮助都将不胜感激。我更喜欢点击重置按钮,页面重置和图像(图片2-5)当页面加载时,我可以完全删除混洗按钮。
发布于 2019-06-05 02:01:07
由于我有一个工作版本,我将提供细节作为解决方案(这对于评论来说太多了),希望一些细节将是纠正你的版本不能正常工作所需的细节。
我使用的三个文件与您的文件一致,我将它们分别命名为so.html、so.js和so.css。
HTML:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>SO Test</title>
<link rel="stylesheet" type="text/css" href="/test/css/so.css" />
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script language="JavaScript" src="/test/script/so.js"></script>
</head>
<body>
<div class="container shuffleImg">
... <!-- here I named the images uniquely, "Image 1" through "Image 8";
otherwise same as your code -->
</div>
<button class="btn btn-danger" id="go">Shuffle</button>
<hr>
<button class="btn btn-danger" onClick="window.location.reload(true);">Reset</button>
</body>
</html>JS:
//card flip function start
$(function() {
// No change here to your first definition of this function
});
function rotateCard(btn) {
//No change to this function
}
// card flip funtion end
// I hoisted shuffle out of the next $(function() ...) block since I use it again later
function shuffle() {
// No change to function content
}
//random function start
$(function() {
$("#go").on("click", shuffle);
});
$(window).load(function() {
shuffle()
});
//random funtion endCSS我根本没有改变。
下面是一个使用以下代码的测试站点:Test。
编辑:我也在使用最新的jQuery库,因为主要的供应商都提供了“最新”的链接。我没有注意到您的jQuery代码中有什么特别的地方,但您的问题可能是您包含了旧的和/或非官方版本。
编辑:根据评论,您已经指出您使用的是jQuery的“超薄”包。顾名思义,“超薄”包并不包含jQuery的几个特殊特性。请看这个SO问题,例如:What are the differences between normal and slim package of jQuery。特别是,不支持您使用的动画功能,也不支持.load函数。因此$(window).load(...)将失败。在浏览器中查看开发控制台可以看到这一点。
https://stackoverflow.com/questions/56434720
复制相似问题