我正在开发一款可向用户展示调查问题的应用。标记看起来像这样:
<body>
<div class="question" id="q1">
Question 1
</div>
<div class="question" id="q2">
Question 2
</div>
<!-- etc -->
</body>
我想使用jQuery从DOM构造JavaScript对象,因此在Survey
构造函数中,我使用each()
方法遍历jQuery集。问题是在回调函数中我无法获得对Survey
对象的引用,以便将每个Question
对象附加到Survey.questions
数组。如何获得对Survey
对象的引用?有没有办法将附加参数(例如对Survey
对象的引用)传递给回调函数?
function Survey() {
this.questions = new Array;
$('.question').each(function(i) { (/* Survey object */).questions.push(new Question(this)); });
}
function Question(element) {
this.element = $(element);
}
答案 0 :(得分:31)
在迭代问题之前,您应该能够创建对调查的引用。
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("<li>" + this.element.text() + "</li>");
});
上的工作示例
答案 1 :(得分:18)
虽然这可能不是相关的答案,但是问题是如何将参数传递给jQuery各个函数。
第一种方法:使用部分功能 - more on this 从closure library
var param = 'extra-param';
var partial = function(fn, var_args) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
// Clone the array (with slice()) and append additional arguments
// to the existing arguments.
var newArgs = args.slice();
newArgs.push.apply(newArgs, arguments);
return fn.apply(this, newArgs);
};
};
$(['joe','james','rick','kirk']).each(partial (function (index,value) {
alert(arguments.length);
alert(arguments[0]);
},param));
第二种方法是
$。每个函数可以使用2个参数Index
和Element
(也可以称为this
)
要么
如果您不需要Index
,那么您可以将Array
作为参数传递给$ .each,元素可以称为此
注意:args仅供内部使用 - 通过jQuery
// Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// only used internally.)
each: function( callback, args ) {
return jQuery.each( this, callback, args );
},
将调用each: function( obj, callback, args )
如果将数组作为参数传递,则调用callback.apply( obj[ i ], args );
否则callback.call( obj[ i ], i, obj[ i ] )
;
请注意申请和致电的区别
示例代码:http://jsfiddle.net/dekajp/yA89R/1/
var param = 'rick';
$(['joe','james','rick','kirk']).each(function (arg1 , arg2 ,arg3) {
//alert('[this: '+this +'] [arg1: '+ arg1 +'] [arg2:'+arg2+'] [param:'+param+']');
},['hello 1','hello 2','hello 3']);
参考jQuery每个代码版本1.9
// args is for internal usage only
each: function( obj, callback, args ) {
var value,
i = 0,
length = obj.length,
isArray = isArraylike( obj );
if ( args ) {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break;
}
}
} else {
for ( i in obj ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break;
}
}
}
// A special, fast, case for the most common use of each
} else {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.call( obj[ i ], i, obj[ i ] );
if ( value === false ) {
break;
}
}
} else {
for ( i in obj ) {
value = callback.call( obj[ i ], i, obj[ i ] );
if ( value === false ) {
break;
}
}
}
}
return obj;
},
答案 2 :(得分:9)
我遇到了类似的问题。将param传递给每个函数:
var param1 = "one";
var param2 = "two";
var params = [ param1, param2 ];
$( '#myTable > tbody > tr' ).each(function( index ) {
var param1back = params[0];
var param2back = params[1];
},params);
答案 3 :(得分:1)
//Create extra data
var dataArray = ["A", "B", "C"];
//Send default arguments from jQuery's each (index and item) along
//with our new data argument to a named function handler.
$("#myList").children().each(function(jqIndex, jqItem)
{
listItemsHandler(jqIndex, jqItem, dataArray[jqIndex]);
});
//Named function handler accepting 3 arguments.
function listItemsHandler(index, item, data)
{
console.log(index + " " + item + " " + data);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol id = "myList">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>