我有一个可拖动的表,我正试图为它拖动一个tr,我想把它放到其中一个可拖放的表中。代码按预期工作,但我想使此代码成为动态代码。这怎么可能呢?以下是我的代码
jQuery('#pipeline_lead_card_table_1').droppable({
tolerance: 'pointer',
drop: function(event, ui) {
jQuery('#pipeline_lead_card_table_1 .pipeline_lead_card_table
tbody').append(ui.helper.children());
}
});
jQuery('#pipeline_lead_card_table_2').droppable({
tolerance: 'pointer',
drop: function(event, ui) {
jQuery('#pipeline_lead_card_table_2 .pipeline_lead_card_table
tbody').append(ui.helper.children());
}
});
jQuery('#pipeline_lead_card_table_3').droppable({
tolerance: 'pointer',
drop: function(event, ui) {
jQuery('#pipeline_lead_card_table_3 .pipeline_lead_card_table
body').append(ui.helper.children());
}
});
jQuery('#pipeline_lead_card_table_4').droppable({
tolerance: 'pointer',
drop: function(event, ui) {
jQuery('#pipeline_lead_card_table_4 .pipeline_lead_card_table
tbody').append(ui.helper.children());
}
});
我想让这段代码动态化,因为我不知道会动态生成多少个表。
发布于 2018-07-28 03:56:01
根据我的评论,您可以像这样简化代码:
(function($) {
function makeDrop($o) {
$o.droppable({
tolorance: "pointer",
drop: function(e, ui) {
$(".pipeline_lead_card_table tbody", this).append(ui.helper.children());
}
});
}
makeDrop($('#pipeline_lead_card_table_1'));
makeDrop($('#pipeline_lead_card_table_2'));
makeDrop($('#pipeline_lead_card_table_3'));
makeDrop($('#pipeline_lead_card_table_4'));
})(jQuery);
不,如果你的每个表都有一个类属性,例如:can-change
,你可以进一步简化代码:
(function($) {
function makeDrop($o) {
$o.droppable({
tolorance: "pointer",
drop: function(e, ui) {
$(".pipeline_lead_card_table tbody", this).append(ui.helper.children());
}
});
}
makeDrop($('.can-change'));
})(jQuery);
https://stackoverflow.com/questions/51540584
复制相似问题