我这里有一长串jquery幻灯片函数:
不同的ID,但行为相同。
代码:
/*DataTables*/
$(document).ready( function() {
$('#all-active-users').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#all-deactivated-users').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#all-ppmps').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#agency-users').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#all-ppmps-by-agency').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#ppmp-form').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#all-agencies').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#ppmp-uploaded-files').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
$(document).ready( function() {
$('#agency-types').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
我想知道的是如何让它简短而精确。
我试过这样的事,但没有运气:
$(document).ready( function() {
$('#all-active-users').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
$('#all-deactivated-users').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
每当我试图缩短时,代码就会死掉。
任何变通办法都将受到赞赏。感谢。
答案 0 :(得分:3)
为每个人分配一个说mySlider
$(document).ready( function() {
$('.mySlider').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
} );
答案 1 :(得分:3)
如果您的插件支持多个选择器,请尝试以下操作:
$(document).ready(function () {
$('#all-active-users, #all-deactivated-users, #all-ppmps, #agency-users, #all-ppmps-by-agency, #ppmp-form, #all-agencies, #ppmp-uploaded-files, #agency-types')
.dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
});
您也可以尝试使用each()
:
$(document).ready(function () {
$('#all-active-users, #all-deactivated-users, #all-ppmps, #agency-users, #all-ppmps-by-agency, #ppmp-form, #all-agencies, #ppmp-uploaded-files, #agency-types')
.each(function () {
$(this).dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
});
});
答案 2 :(得分:2)
您可以将规则应用于所有编程。不要重复自己。
注意你的所有方法是如何做同样的事情的?所以你要做的就是封装它,然后简单地为每次更改调用它。
制作这样的函数:
var doSomething = function(selector){
$(selector).dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
};
现在我们有一个函数可以调用我们喜欢的任何选择器:
$(document).ready( function() {
var doSomething = function(selector){
$(selector).dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers"
} );
};
doSomething('#all-active-users');
doSomething('#all-deactivated-users');
} );
几乎在那里,但我们每次都会通过调用它来重复自己。那么为什么不这样做:
$.each(['#all-active-users', '#all-deactivated-users'], function(index, value) {
doSomething(value);
});