我需要一些帮助来修复此代码的错误:
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或undefined,而不是Function对象。 第46行是这一行:
$('#random').attr('src',image[x]);
我发现此代码的问题的链接是here
修改
以下是我整个计划的代码here
有人可以帮我吗?
答案 0 :(得分:4)
在文件<head>
块
<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>
答案 1 :(得分:1)
在页面顶部和head标签中包含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]);
})
答案 2 :(得分:1)
尝试这个..希望它的帮助
<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>
答案 3 :(得分:0)
您的代码中存在一些错误,这是一个有效的示例......
确保jQuery is included on your page
$(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);
});