简体   繁体   English

无法让jQuery将文本插入所有元素

[英]Can't get jQuery to insert text into all elements

I want my JS to insert text into span tags. 我希望我的JS将文本插入span标签。 The structure of my html is below. 我的html的结构如下。

<h4>Question 1</h4>
<div class="q-header"><span></span></div>
<div class="q-header"><span></span></div>
<div class="q-header"><span></span></div>
<h4>Question 2</h4>
<div class="q-header"><span></span></div>
<div class="q-header"><span></span></div>
<h4>Question 3</h4>
<div class="q-header"><span></span></div>
<div class="q-header"><span></span></div>
<div class="q-header"><span></span></div>
<h4>Question 4</h4>

The way the function should do this is by finding the first h4 element and selecting all the elements between that h4 and the next h4. 函数执行此操作的方式是找到第一个h4元素,然后选择该h4和下一个h4之间的所有元素。 I then use the each() function to insert text into the selected span tags. 然后,我使用each()函数将文本插入所选的span标签。 With a while loop I repeat this for all elements on the page until no more h4 elements can be selected. 通过while循环,我对页面上的所有元素重复此操作,直到无法再选择h4元素为止。

The above HTML should appear in the browser as: 上面的HTML在浏览器中应显示为:

Question 1
Question 1a
Question 1b
Question 1c
Question 2
Question 2a
Question 2b
Question 3
Question 3a
Question 3b
Question 3c
Question 4

Currently, only the manually entered main question titles show and the subquestions don't show. 当前,仅显示手动输入的主要问题标题,而不显示子问题。

var alphabet = ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

//Function names all questions
function nameQuestions(){
    var cQuestion = 1;
    var currentQuestion = $('h4:nth-child(' + cQuestion + ')');
    var cSubQuestions = 1;
    while (true){
    currentQuestion.nextUntil('h4').each(function(){
        $(this).children('span').text('Question ' + cQuestion + alphabet[cSubQuestions]);
        cSubQuestions+=1;
    });
    cQuestion+=1;
    currentQuestion = $('h4:nth-child(' + cQuestion + ')');
    cSubQuestions = 1;
    if (currentQuestion.length < 1){ break; }
    }
}

fiddle 小提琴

The following update will get you further: http://jsfiddle.net/ov4oa3dw/15/ You will need additional code to capture the items that are not followed by an h4. 以下更新将使您更进一步: http : //jsfiddle.net/ov4oa3dw/15/您将需要其他代码来捕获h4之后没有的项目。

$(document).ready(function(){
var alphabet = ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

//Function names all questions
function nameQuestions(){
    //since you need the index number of each h4, you can save them to an array
    var questions = $("h4");
    for(var cQuestion=0; cQuestion<questions.length; cQuestion++){
        var currentQuestion = questions[cQuestion];
        var cSubQuestions = 1;

        //your while(true) is not needed since each() will iterate over the set
        $(currentQuestion).nextUntil('h4').each(function(){
            $(this).children('span').text('Question ' + cQuestion + alphabet[cSubQuestions]);
            cSubQuestions+=1;
        });
    }
}
nameQuestions();
});

DEMO DEMO

You can simplify your code as follows. 您可以按以下方式简化代码。 Even if you do need a function your can call later, the code would still be much simpler. 即使确实需要稍后可以调用的函数,代码也仍然要简单得多。

$(document).ready(function(){
    var alphabet = ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    $('h4').each(function(i) {
        $(this).nextUntil('h4').each(function(j) {
            $(this).children('span').text('Question ' + (i+1) + alphabet[j+1]);
        });
    });
});

Adopted from @terrywb answer I just modified code a bit and it worked. 来自@terrywb的答案我刚刚修改了一下代码,它起作用了。

$(document).ready(function(){
var alphabet = ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']


//Function names all questions
    function nameQuestions(){
        var questions = $("h4");
        for(var cQuestion=0; cQuestion<questions.length; cQuestion++){
            var currentQuestion = questions[cQuestion];
            var cSubQuestions = 1;
            $(currentQuestion).nextUntil('h4').each(function(){
                $(this).children('span').text('Question ' + parseInt( cQuestion+1) + alphabet[cSubQuestions]);
                cSubQuestions+=1;
            });
        }
    }
    nameQuestions();
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM