刷新时随机图像
Random Image on Refresh
我需要一些帮助来修复此代码上的错误:
var image = new Array ();
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var size = image.length
var x = Math.floor(size*Math.random())
$('#random').attr('src',image[x]);
当我运行HTA(超文本应用程序)时,我得到的错误是:线路:46错误:属性"$"的值为null或未定义,而不是Function对象。第46行是这一行:
$('#random').attr('src',image[x]);
我发现这个代码的问题的链接在这里
编辑
这是我整个程序的代码
有人能帮我吗?
在文件<head>
块中添加Jquery库
<script src="//code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script>
$(window).load(function(){
var image = new Array ();
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var size = image.length
var x = Math.floor(size*Math.random())
$('#random').attr('src',image[x]);
});
</script>
演示
在页面顶部和标题选项卡中包含jquery:
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
再试一次:
$(function(){
var image = new Array ();
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var size = image.length
var x = Math.floor(size*Math.random())
$('#random').attr('src',image[x]);
})
试试这个。。希望对有所帮助
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(window).load(function(){
var image = new Array ();
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var size = image.length
var x = Math.floor(size*Math.random())
$('#random').attr('src',image[x]);
});
</script>
您的代码中有几个错误,下面是一个工作示例。。。
确保jQuery包含在您的页面中
$(document).ready({
// jQuery has now loaded on the page
var image = [];
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var src = image[Math.floor(Math.random() * image.length)];
$('#random').attr('src',src);
});
链接到示例
相关文章:
- 在固定位置显示随机图像
- 阵列中随机图像的问题
- 使用JS按顺序显示图像,而不是随机显示
- 使用CSS、HTML和Javascript在随机图像的页面上调整图像大小
- Jquery/Javascript:通过按下按钮显示数组中的随机图像
- jQuery:将图像随机填充到页面上的另一个图像上
- 使用CSS在屏幕上居中显示图像-随机屏幕/图像尺寸
- JavaScript或JQuery代码,将jpg图像随机放置在表中
- 如何在没有JQ的情况下将图像随机放入单元
- 如何移动一个图像随机当你点击另一个图像使用html &javascript
- 如何通过修改此代码使图像随机但不重复
- 我编写了一段javascript代码,可以将数组中的图像随机显示到照片网格中,但我不希望图像重复
- 多个画布图像随机出现
- 使图像随机移动,点击
- javascript中的图像随机化器
- 如何使用javascript将图像随机插入到表中
- 改变图像随机点击而不重复
- jQuery图像谜题:图像随机化问题
- 质量中的图像随机化
- 如何实现“使用jQuery的不可能简单的图像随机化器”.没有jQuery