周梦康 发表于 2017-12-07 1767 次浏览 标签 : javascript

规划一下功能,点击删除该<li>dom

<li>
  <span class="ajax-del-opt" 
    data-ajax-url="xxx" 
    data-jump-url="xxx" 
    data-done="$(this).closest('li').remove()">删除</span>
<li>

通过 Function 来做回调

$(document).on('click','.ajax-del-opt', function (event) {
  event.preventDefault();
  let url = $(this).data('ajax-url');
  let done = $(this).data('done');
  let self = this;

  Ajax.sendData({
    type: 'POST',
    url: url,
    success: (result) => {
      if (result.errcode == 0) {
          const fn = new Function(done);
          fn.call(self);
        }
      } else {
        //...
      }
    },
    dataType: 'json'
  });
});

评论列表