Javascript 向传递给函数的jQuery包装元素添加元素
对不起,我没有说得那么清楚。这是我原稿的完整版本,力求更具体 让打电话的人Javascript 向传递给函数的jQuery包装元素添加元素,javascript,jquery,Javascript,Jquery,对不起,我没有说得那么清楚。这是我原稿的完整版本,力求更具体 让打电话的人 myFunction ( $('#tableOne'), anInteger ) ); 至于被叫人: function myFunction ( theTable, theIndex ) { /* pseudo: Without using the first parameter, I could write for instance : IT WORKS --> $('#tableOne’ +
myFunction ( $('#tableOne'), anInteger ) );
至于被叫人:
function myFunction ( theTable, theIndex ) {
/* pseudo:
Without using the first parameter, I could write for instance :
IT WORKS --> $('#tableOne’ + ‘ #line' + theIndex ).val(‘blabla’) ;
Now I want to use the first parameter…
just replace the first part of the wrapped set —i.e $('#tableOne —
with the argument —i.e. theTable
*/
}
这些都不起作用:
theTable + ‘#line' + theIndex. val(‘blabla’) ;
$( theTable + ‘#line' + theIndex).val(‘blabla’) ;
theTable.append( ‘#line' + theIndex).val(‘blabla’) ; // just to make sure…
$(theTable).append( ‘#line' + theIndex).val(‘blabla’) ; // just to make sure…
$(theTable) + ‘#line' + theIndex. val(‘blabla’) ;
在被调用方中实现这一点的正确语法是什么?我不确定是否理解您的意图,因此假设您在myJQArray中有一堆jQuery包装的元素,并且希望它们由myOtherElement作为父元素,您可以执行以下操作:
$('#somePrefix')。追加($('#ArrayOne'))
当您试图传递$('#ArrayOne'),$('#somePrefix')
它将作为jquery对象传递。以这种方式传递参数可能没有任何限制,只要myFunction
准备好处理此类参数
但是,当您在寻找模块化设计时,最好传递通用参数,而不是直接引用jquery对象。同样的函数也可以用来处理其他jquery对象
你可以这样做
myFunction({
arg1 :$('#ArrayOne'),
arg2:$('#somePrefix')
})
muFunction(options){ // where options is an object which have arg1 & arg2 as key
// use arg1 & arg2 like this
var _m = options.arg1;
var _n = options.arg2;
// Rest of code
}
现在在myFunction
中定义如下
myFunction({
arg1 :$('#ArrayOne'),
arg2:$('#somePrefix')
})
muFunction(options){ // where options is an object which have arg1 & arg2 as key
// use arg1 & arg2 like this
var _m = options.arg1;
var _n = options.arg2;
// Rest of code
}
您可以使用jquery映射函数来实现这一点
function myFunction (myJQArray, myOtherElement) {
return $.map(myJQArray, function(myObj, index) {
var combined = myObj.val() + myOtherElement.val()
var withindex = combined + (index+1).toString();
return withindex;
}).join(' ');
}
我自己的解决方案基本上包括在将传递的表对象合并到一个模仿原始包装id的字符串中后重新包装它: 1) 应该检索jQuery对象的id
IDString = theTable.attr ('id);
2) 使用theID,合成一个包装集,如下所示:
$('#'+ IDString + ' #line' + theIndex).val('blabla') ; // Look out for the space before #line
我工作,虽然我怀疑有一种更优雅的方式…请稍等:我正在考虑这一切,为了清晰起见,可能会编辑我的原始帖子。请现在查看我编辑的帖子。展望未来…尽我所能为自己解决它:请看一看我自己的答案,看看是否可以改进。请稍等:我正在考虑这一切,为了清晰起见,可能会编辑我的原始帖子。请现在查看我编辑的帖子。展望未来…尽我所能为自己解决它:请看一看我自己的答案,看看是否可以改进。请稍等:我正在考虑这一切,为了清晰起见,可能会编辑我的原始帖子。请现在查看我编辑的帖子。展望未来……尽我所能为自己解决这个问题:请看看我自己的答案,看看是否可以改进。