gpt4 book ai didi

javascript - JQuery 循环仅评估第一项

转载 作者:行者123 更新时间:2023-11-30 19:47:19 26 4
gpt4 key购买 nike

我已经完成了与这个问题类似的大部分答案,并且尝试了不同的实现,但没有成功。您会在我的代码中看到,在我尝试过不同方法的地方,有很多东西被注释掉了。但是,没有一个对我有用。

 <script>
function GenerateRandomColor(ele) {
var colors = ["red", "orange", "blueviolet", "darkorchid", "midnightblue", "lightblue", "aqua", "darkgreen","lime", "orangered"];
ele.css('color', colors[Math.floor(Math.random()*9)]);
ele.css('font-weight', bold);
//return;
}
$(document).ready(function () {
var elements = $(".impt");
var colors = ["red", "orange", "blueviolet", "darkorchid", "midnightblue", "lightblue", "aqua", "darkgreen","lime", "orangered"];
for(var i=0; i<elements.length; i++){
var element = elements.eq(i);
//do something with element'
element.css('color', colors[Math.floor(Math.random()*9)]);
element.css('font-weight', bold);
//continue;
//GenerateRandomColor(element);
}
/*var spanObjs = document.querySelectorAll('.impt');
Array.prototype.forEach.call(spanObjs, function(elements, index) {
GenerateRandomColor($(elements));
});*/
/*$('.impt').each(function(i, obj) {
GenerateRandomColor($(obj));
});*/
});
</script>

我的目标是:我有一个段落,其中有多个跨度以强调其中的一些重要单词。所以 span 都有类 impt。我想让文本颜色随机,所有单词都加粗。

所有的实现都给我相同的结果。第一个跨度被着色并加粗,仅此而已。

最佳答案

需要加粗引号

https://jsfiddle.net/x4rsyh8n/

您应该打开控制台,以便日后查看 javascript 错误。

<script>
function GenerateRandomColor(ele) {
var colors = ["red", "orange", "blueviolet", "darkorchid", "midnightblue", "lightblue", "aqua", "darkgreen","lime", "orangered"];
ele.css('color', colors[Math.floor(Math.random()*9)]);
ele.css('font-weight', 'bold');
//return;
}
$(document).ready(function () {
var elements = $(".impt");
var colors = ["red", "orange", "blueviolet", "darkorchid", "midnightblue", "lightblue", "aqua", "darkgreen","lime", "orangered"];
for(var i=0; i<elements.length; i++){
var element = elements.eq(i);
//do something with element'
element.css('color', colors[Math.floor(Math.random()*9)]);
element.css('font-weight', 'bold');
//continue;
//GenerateRandomColor(element);
}
/*var spanObjs = document.querySelectorAll('.impt');
Array.prototype.forEach.call(spanObjs, function(elements, index) {
GenerateRandomColor($(elements));
});*/
/*$('.impt').each(function(i, obj) {
GenerateRandomColor($(obj));
});*/
});
</script>

关于javascript - JQuery 循环仅评估第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54830409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com