1

我正在尝试将脚本转换为函数,因为一遍又一遍地重复相同的代码非常烦人。

该脚本选择了一个容器,该容器将很快容纳一个新的手风琴。容器内的每个手风琴都有一个唯一的数据排序 ID。(它需要从 1 到 10 排序)然后它检测是否有任何 id 小于或大于它的手风琴,并在它们之间添加自己。

该脚本可以正常工作,而无需它位于函数内部。

这个没有函数的脚本可以正常工作

var $container = $('.panel-group[data-group-holder="' + dataPlace + '"]'),
    $d = $container.find('div.panel-default'),
    $n = $('<div class="panel panel-default dragged-true" data-sort="' + dataSort + '"><div class="panel-heading"><h4 class="panel-title"><a class="accordion-toggle sub" data-sort="' + dataSort + '" data-toggle="collapse" href="#main-' + dataSort + '"><div class="remove set"><i class="icon-remove"></i></div>' + dataHtml + ' <b>(<span class="sub-count">0</span>)</b><i class="icon-angle-down"></i></a></h4></div><div id="main-' + dataSort + '" class="panel-collapse in"><div class="panel-body" data-accept="' + dataSort + '"></div></div></div>');
var $a = $d.filter(function () {
    return $(this).data('sort') < dataSort;
}).last();

$(that).parents('div:eq(0)').remove();
/*alert($('.draggable-groups .main-groups[data-sort="' + dataSort + '"]').parents().html());*/

if ($a.length) $a.after($n);
else $container.prepend($n);

此功能无法正常工作(无法排序)

    function placeSort(thisContainer, containerFind, placeHtml, returnCompareOneFind, returnCompareOne, returnCompareTwo, removeClone) {
        var $container = $(thisContainer),
            $d = $container.find(containerFind),
            $n = $(placeHtml);

        if(returnCompareOneFind == "false") {
            var $a = $d.filter(function () {
                return $(this).data(returnCompareOne) < returnCompareTwo;
            }).last();
        } else {
            var $a = $d.filter(function () {
                return $(this).find(returnCompareOneFind).data(returnCompareOne) < returnCompareTwo;
            }).last();
        }

        removeClone;

        if ($a.length) $a.after($n);
        else $container.prepend($n);
    }

function addGroupAccordion(that, dataSort, dataPlace, dataHtml) {
    var thisContainer = '.panel-group[data-group-holder="' + dataPlace + '"]';
    var containerFind = 'div.panel-default';
    var placeHtml = '<div class="panel panel-default dragged-true" data-sort="' + dataSort + '"><div class="panel-heading"><h4 class="panel-title"><a class="accordion-toggle sub" data-sort="' + dataSort + '" data-toggle="collapse" href="#main-' + dataSort + '"><div class="remove set"><i class="icon-remove"></i></div>' + dataHtml + ' <b>(<span class="sub-count">0</span>)</b><i class="icon-angle-down"></i></a></h4></div><div id="main-' + dataSort + '" class="panel-collapse in"><div class="panel-body" data-accept="' + dataSort + '"></div></div></div>';
    var returnCompareOneFind = "false";
    var returnCompareOne = 'sort';
    var returnCompareTwo = dataSort;
    var removeClone = $(that).parents('div:eq(0)').remove();

    placeSort(thisContainer, containerFind, placeHtml, returnCompareOne, returnCompareTwo, removeClone);
}

$(document).on('click','.draggable-groups .main-groups',function(){
        var dataSort = $(this).attr("data-sort");
        var dataPlace = $(this).parents('div:eq(1)').attr("data-group");
        var dataHtml = $(this).html();
        var that = $(this);
        addGroupAccordion(that, dataSort, dataPlace, dataHtml);
        var groupHolder = dataPlace;
        countChildren(groupHolder);
    });

HTML

<div class="panel panel-default">
  <div class="panel-heading main">
    <h4 class="panel-title">
      <a class="accordion-toggle collapsed main side-js" data-parent="#main-panel" data-panel="00" data-toggle="collapse" href="#panel-00">
        <span>0</span>Tellija kulud 
        <b>(<span class="cat-count">0</span>)</b>
        <i class="icon-angle-down"></i>
      </a>
    </h4>
  </div>
  <div id="panel-00" class="panel-collapse collapse">
    <div class="panel-body">

      <div class="panel-group" data-group-holder="00">

      </div>

    </div>
  </div>
</div>

<h3>Main groups</h3>
  <div class="row draggable-groups" data-group="00">
    <div class="col-md-6">
      <div class="main-groups" data-sort="01">01</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="02">02</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="03">03</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="04">04</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="05">05</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="06">06</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="07">07</div>
    </div>
    <div class="col-md-6">
      <div class="main-groups" data-sort="08">08</div>
    </div>
  </div>

创建一个 jsFiddle 会花费太多时间来重新创建它。我很抱歉缺少它。

4

1 回答 1

2

您的问题可能归结为试图通过的问题this

试试这个:

function addGroupAccordion(self, dataSort, dataPlace, dataHtml) {
    ...
    var removeClone = $(self).parents('div:eq(0)').remove();
    ...
}

$(document).on('click','.draggable-groups .main-groups',function(){
    ...
    var self = this; // 'self' is more standardized
    addGroupAccordion(self, dataSort, dataPlace, dataHtml);
    ...
});

这里发生的事情是,当您var that = $(this);$().on函数中进行设置时,您正在存储包含 的 jQuery 对象this,然后当您尝试通过 using 在addGroupAccordion函数中使用它时$(that),您要求 jQuery 执行:$($(this))

于 2013-10-09T19:33:18.667 回答