- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的任务是获取 jsonObj 中的 3 个不同颜色列表并将它们放入 <ul>
。它们每秒只能出现一个。为了 fiddle 的缘故,我每 5 秒放一次。
我还没有到达第二个或第三个颜色列表,因为虽然我可以列出我的第一个颜色列表,但它们附加在我为它们创建的 listItem 之外。它吐出的代码是:
var jsonObj = '{"one":["red","green","blue"], "two":["red","green","blue"], "three":["orange","purple","hotpink"]}',
object = JSON.parse(jsonObj),
cOne = object.one,
cTwo = object.two,
cThree = object.three,
i = 0,
timer;
$('body').append('<ul/>');
timer = setInterval(function() {
$.each(cOne, function() {
var list = $('body ul'),
listItem = $(list).append('<li>'),
html = $(listItem).append(cOne[i]);
if (i < cOne.length) {
i++;
$(cOne[i]).split("");
list.append(html);
} else if (i = cOne.length) {
i = 0;
}
});
}, 5 * 1000);
timer;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
也可通过 https://jsfiddle.net/ep76ba3u/ 获取
它的作用:
<ul>
<li></li>
"red"
<li></li>
"blue"
</ul>
它应该是什么样子:
<ul>
<li>red</li>
<li>blue</li>
</ul>
我尝试过重新安排一切。我尝试过使用wrapper、innerWrap。我尝试过仅使用 text() 和其他一些方法。我凌晨 3 点开始工作,现在已经凌晨 5 点了……脑子都炸了。任何关于如何插入这一行动的想法都值得赞赏。
最佳答案
您无法附加部分 html,这就是为什么 $(list).append('<li>')
立即关闭<li>
.
并且您不应该在循环中修改标记。它令人讨厌且性能不佳。
检查一下您的代码的这种方法:
var jsonObj = '{"one":["red","green","blue"], "two":["red","green","blue"], "three":["orange","purple","hotpink"]}',
object = JSON.parse(jsonObj),
iteration = 0,
timer;
$('body').append('<div id=container>');
//a few utilities, because I don't want to repeat myself all over the place:
var string = value => value == null ? "" : String(value);
var wrapInNode = nodeName => value => `<${nodeName}>${ string(value) }</${nodeName}>`;
//here I create a few utility-methods that will build my markup:
var li = wrapInNode('li');
var ul = wrapInNode('ul');
var header = wrapInNode('h4');
timer = setInterval(function() {
//building the complete markup and adding it at once
var blocks = [],
//how many rows should I show in this iteration
numRowsLeft = ++iteration,
//getting this result is just a nice sideeffect of using `every()` instead of `forEach()`
//to short-curcuit the loop
done = Object.keys(object)
.every(function(key) {
//this line makes the title to be added with as a distinct iteration and not with the first item,
//check out what happens when you remove it
--numRowsLeft;
var rows = object[key]
//shorten the Array to numRowsLeft, if necessary
.slice(0, numRowsLeft)
//wrap each item in a li-node with my predefined utility-function
.map(li);
numRowsLeft -= rows.length;
//building the markup for this block
blocks.push(header(key) + ul(rows.join("")));
//here I'm short circuiting the loop. to stop processing the other keys on Object
return numRowsLeft > 0;
});
$('#container').html(blocks.join(""));
if (done) {
clearInterval(timer);
}
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
并始终显示标题,而仅添加点:
var jsonObj = '{"one":["red","green","blue"], "two":["red","green","blue"], "three":["orange","purple","hotpink"]}',
object = JSON.parse(jsonObj),
iteration = 0,
timer;
$('body').append('<div id=container>');
var string = value => value == null ? "" : String(value);
var wrapInNode = nodeName => value => `<${nodeName}>${ string(value) }</${nodeName}>`;
var li = wrapInNode('li');
var ul = wrapInNode('ul');
var header = wrapInNode('h4');
timer = setInterval(function() {
var numRowsLeft = ++iteration,
blocks = Object.keys(object)
.map(function(key) {
var rows = object[key]
.slice(0, numRowsLeft)
.map(li);
numRowsLeft -= rows.length;
return markup = header(key) + ul(rows.join(""));
});
$('#container').html(blocks.join(""));
// If I'd had room to show even more rows, then I' done
if (numRowsLeft > 0) {
clearInterval(timer);
}
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
关于javascript - JSON Split - 在间隔期间将数组文本放入 li 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43069359/
好吧,标题说明了一切。我的 javascript 函数构建一些“ block ”并使用 和 创建它们。问题是,它删除了所有其他 在网页上。 编辑:(好吧,我假设这个函数是造成这个的原因。删除我的意思是
这个 HTML 结构有效吗? Item 1 Item 2 Item 3 Item 4 Item 5 Item 6
我正在尝试在 UL 中添加标题因为你不能在 UL 中使用 H2如果按照我展示的方式使用它可以吗? News roll News 1 News 2 我的一位 friend 告诉我
这是我的 代码风格。 li{ font-family: Arial, sans-serif; font-size: 100%; color: black; display : list-item;
我正在使用 a 进行水平导航,感谢问题:Separators For Navigation ,我读到了使用 li + li 设置基于图像的元素分隔符的好方法。 (伪?)选择器。 在我的代码中,我为
我有一个 Div 容器,其中包含 4 我正在做的是在前面加上 进入并删除最后一个 我很容易使用 $('#news-ul li:last').remove() $('#news-ul').prepe
这是 html: menu item menu item menu item menu item menu item 如果我按“a”链接,它会将“当
我有 ul-li 列表 1 2 3 A B 我可以在列表中使用纯 css 最后一个选择器“.digit”吗? .digit 的数量未知 ul li.digit:nth-last-ch
我有这种情况,1- 我想删除一个带有两个图标的 li,它作为一个跨度在里面:一个是选中或取消选中相应的 li,另一个是删除那个 li。2- 我还想通过单击图标(选中图标)同时单击 li 来选中或取消选
如果标题不好理解,我来解释一下... 我有一个 css/jquery 切换菜单。按“+”号时它变大,按“-”号时它变小。我使几个元素的填充、边距和高度在切换/单击时发生变化。菜单顶部充满了链接,然后当
我有一个脚本 ( JsFiddle here ) 检测 li block 元素何时在页面上垂直居中并为其分配一个 .centered 类以使其更大通过 CSS。 .centered { hei
代码在这里:http://jsfiddle.net/C5mTf/49/我不知道为什么。下面的代码有问题吗? $("#menu").on('click','li',function (){ va
我为这个问题苦苦挣扎了几个小时,但仍然无法解决问题。 我有这样的 html 代码: aaaa11 bbbb11 我想知道如何使用 css 让 每里一行显示。但是 标签仍然具有垂
我需要使用水平缩放的 ul > li > ul 的组合来创建一个菜单。每个 ul 都应该有一个 max-height 并在溢出时滚动。悬停在 li 上时,如果里面有另一个 ul 标签,它应该会在右侧打
我了解到,如果我指的是 在 内或 在 CSS 中我应该使用 ol > li或 ul > li但是一旦我忘记在 ol 和 li 之间放置标志,我发现它仍然有效。执行此操作的正确方法是什么? 最佳答案
Question 1 Answer: $('#addquestion').click(function() { var $question_number
我如何在 中显示我的数组值? 我的代码: HTML JS var myList = [ ['1','one'], ['1','two'], ['1','th
我在使用代码编辑器时遇到了问题所以就在这里 Item1 Item 1.1 Item 1.2 Item2
我正在为我的网站创建响应式移动版本。正如您在此 jsfiddle 中看到的那样, 当用户将鼠标悬停在 上时项,菜单打开但覆盖了 而不是展开菜单。 我已经添加了 display: block在不同的地
我正在尝试创建一个网站,但我偶然发现了一个小问题,我正在使用 bootsnipp(一个侧面菜单)中的一个片段,我无法让它工作,所以链接将覆盖整个 LI,而不是你必须按自己的文字如果我改变它,那么它会坐
我是一名优秀的程序员,十分优秀!