Javascript 将数组值插入Div框
我希望用户在单击按钮时将数组中的值插入五个div框之一。代码在firebug上没有错误,但是当我单击按钮时,什么也没有发生。我的代码有什么问题。下面是JSFIDLE示例 下面是相关的js代码Javascript 将数组值插入Div框,javascript,jquery,html,Javascript,Jquery,Html,我希望用户在单击按钮时将数组中的值插入五个div框之一。代码在firebug上没有错误,但是当我单击按钮时,什么也没有发生。我的代码有什么问题。下面是JSFIDLE示例 下面是相关的js代码 var news = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]; $(document).ready(fu
var news = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20"];
$(document).ready(function() {
$('.button').click(function() {
var article_id = 1;
var number = Math.floor(Math.random() * 5) + 1;
$('.class_' + number).html('news[article_id]');
article_id++;
});
});
您错误地分配了类,并且您的HTML使用了
-
,但是您的JavaScript使用了
作为类名
这是一个有效的JSFIDLE:您错误地分配了类,并且您的HTML使用了
-
,但是您的JavaScript使用了
作为类名
下面是正在工作的JSFIDLE:在html中使用“class-1”,在javascript中使用“$('.class\u'+number)”,这将导致class\u1。因此,类1和类1不匹配。在html中,您使用的是“类1”,而在javascript中,您使用的是“$('.class.+number)”,这将导致类1。因此,类1和类1中存在不匹配的情况:
news[article\u id]
once-fix-hyphens由于变量周围的引号,上面的代码无法工作:news[article\u id]