Pass additional parameters to jQuery each() callback

后端 未结 4 694
梦谈多话
梦谈多话 2020-12-24 05:32

I\'m working on an app that will present surveys to the user. The markup looks something like this:


    
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 06:06

    You should be able to create a reference to your survey before you iterate over the questions.

    function Survey() {
        this.questions = new Array();
        var survey = this;
        $('.question').each(function(i) {
            survey.questions.push(new Question(this));
        });
    }
    
    function Question(element) {
        this.element = $(element);
    }
    
    var survey = new Survey();
    
    $.each(survey.questions, function() {
        $("ul").append("
  • " + this.element.text() + "
  • "); });

    Working example on jsfiddle

提交回复
热议问题