Javascript 向类的每个元素添加随机字体

Javascript 向类的每个元素添加随机字体,javascript,jquery,css,arrays,Javascript,Jquery,Css,Arrays,我们一直在努力使类为.book的每个div从数组中获得一个随机字体。这是到目前为止我的代码 var fonts = ['Gloria Hallelujah', 'Sedgwick Ave', 'Gochi Hand', 'Patrick Hand', 'Kalam', 'Rock Salt', 'Neucha', 'Caveat Brush', 'Schoolbell']; var randomfont = fonts[Math.floor(Math.random() * fonts.lengt

我们一直在努力使类为
.book
的每个div从数组中获得一个随机字体。这是到目前为止我的代码

var fonts = ['Gloria Hallelujah', 'Sedgwick Ave', 'Gochi Hand', 'Patrick Hand', 'Kalam', 'Rock Salt', 'Neucha', 'Caveat Brush', 'Schoolbell'];
var randomfont = fonts[Math.floor(Math.random() * fonts.length)];
$(".book").style.fontFamily = randomfont; 
我得到的错误是:
uncaughttypeerror:无法设置未定义的属性“fontFamily”

有人知道我做错了什么吗?

$(“.book”)
返回jquery数组。例如,要设置您将使用的第一个:

$(".book")[0].style.fontFamily = 
您需要循环通过
$(“.book”)
获取DOM元素:

$(".book").each(function() {
    var randomfont = fonts[Math.floor(Math.random() * fonts.length)];
    this.style.fontFamily = randomfont; 
});
(除非您打算将它们全部设置为相同的字体,在这种情况下,在循环外设置
randomfont

$(“.book”)
返回一个jquery数组。例如,要设置您将使用的第一个:

$(".book")[0].style.fontFamily = 
您需要循环通过
$(“.book”)
获取DOM元素:

$(".book").each(function() {
    var randomfont = fonts[Math.floor(Math.random() * fonts.length)];
    this.style.fontFamily = randomfont; 
});

(除非您打算将它们都设置为相同的字体,在这种情况下,请在循环之外设置
randomfont

这是因为您试图直接在jQuery对象而不是DOM元素上设置fontFamily

使用jQuery的
css
方法或使用
get
/array选择器来检索DOM元素

另一方面,您希望在每个元素上执行此操作,因此需要像这样使用
每个

//const font=['Gloria halleujah'、'Sedgwick Ave'、'Gochi Hand'、'Patrick Hand'、'Kalam'、'Rock Salt'、'Neucha'、'cavet Brush'、'Schoolbell';
const fonts=['Helvetica','Arial','Verdana','Courier','Courier New'];
$('.book')。每个(功能(项){
const randomfont=font[Math.floor(Math.random()*font.length)];
this.style.fontFamily=randomfont;
});

测试1
测试2
测试3
测试4
测试5

测试6
这是因为您试图直接在jQuery对象而不是DOM元素上设置fontFamily

使用jQuery的
css
方法或使用
get
/array选择器来检索DOM元素

另一方面,您希望在每个元素上执行此操作,因此需要像这样使用
每个

//const font=['Gloria halleujah'、'Sedgwick Ave'、'Gochi Hand'、'Patrick Hand'、'Kalam'、'Rock Salt'、'Neucha'、'cavet Brush'、'Schoolbell';
const fonts=['Helvetica','Arial','Verdana','Courier','Courier New'];
$('.book')。每个(功能(项){
const randomfont=font[Math.floor(Math.random()*font.length)];
this.style.fontFamily=randomfont;
});

测试1
测试2
测试3
测试4
测试5

测试6
希望这会有所帮助。只需使用
document.getElementById(“”)
调用id,然后设置字体值

var fonts = ['Impact,Charcoal,sans-serif', 'Sedgwick Ave', ' Helvetica', 'Patrick Hand', 'Kalam', 'Rock Salt', 'Neucha', 'Caveat Brush', 'Schoolbell'];
var randomfont = fonts[Math.floor(Math.random() * fonts.length)];
alert(randomfont);
var val=document.getElementById("book").style.fontFamily = randomfont;

希望这会有所帮助。只需使用
document.getElementById(“”)
调用id,然后设置字体值

var fonts = ['Impact,Charcoal,sans-serif', 'Sedgwick Ave', ' Helvetica', 'Patrick Hand', 'Kalam', 'Rock Salt', 'Neucha', 'Caveat Brush', 'Schoolbell'];
var randomfont = fonts[Math.floor(Math.random() * fonts.length)];
alert(randomfont);
var val=document.getElementById("book").style.fontFamily = randomfont;

试试
$(“.book”).css(“字体系列”,随机字体)
请发布您的htmltry
$(.book”).css(“字体系列”,随机字体)请发布您的htmlWon不按类而非id设置OP want。这意味着需要设置多个元素,id应该是唯一的。不会按类而不是id设置OP want。这意味着需要设置多个元素,id应该是唯一的。