我试着把标题转成这样:
Greetings
Introductions
Numbers
(etc.)这方面:
1. Greetings
2. Introductions
3. Numbers
(etc.)所以我写了这个:
var outlineCount = 0
$('.page-template-page-outline .entry-content > h2').prepend('<span class="lesson-number">' + outlineCount++ + '.</span> ')但我得到的却是:
0. Greetings
0. Introductions
0. Numbers
(etc.)我做错什么了?
发布于 2015-04-07 04:07:25
您不需要遍历每个元素,然后将内容放在前面。相反,您要做的是将其应用于jQuery集合中的所有元素。
因此,它将是0,因为outlineCount的值只有在++后缀操作符之后才是1。
$('.page-template-page-outline .entry-content > h2').each(function(i) {
$(this).prepend('<span class="lesson-number">' + (i + 1) + '.</span>')
});在上面的代码中,我们使用在回调中传递的索引,并向其中添加一个索引。不需要单独的柜台。
发布于 2015-04-07 04:07:31
您在"const“字符串前面加上一个”const“字符串,因为'<span class="lesson-number">' + 0 + '.</span> ' u必须知道,在代码中只调用一次前缀。
这也许是解决办法
var outlineCount = 0
$('.page-template-page-outline .entry-content > h2').each(function(){
$(this).prepend('<span class="lesson-number">' + outlineCount++ + '.</span> ');
})发布于 2015-04-07 04:19:21
传递给前置方法的参数在调用该方法之前解析为一个;即
'<span class="lesson-number">' + outlineCount++ + '.</span> '决心:
'<span class="lesson-number">0.</span> '它将被添加到与CSS选择器'.page-template-page-outline .entry-content > h2'匹配的每个元素中。
您需要对返回的每个元素进行迭代,以使方法正常工作。
$('.page-template-page-outline .entry-content > h2').each(function ( index, element ) {
element.prepend('<span class="lesson-number">' + ++outlineCount + '.</span> ');
});https://stackoverflow.com/questions/29483546
复制相似问题