0

我在fieldset. 这包括纯 html、嵌套对象(甚至其他字段集)以及inputselecttextarea对象的值更改。

如果内容已更改,我想更改字段集的边框。以下作品:

$('fieldset[name=qsfs127]').children('input').change(function(){
    $(this).parent('fieldset').css({border:'1px solid red'});
})

这处理输入;我可以将它扩展到选择和文本区域。

问题

  1. 我怎样才能对 html 更改做同样的事情?
  2. 是否可以通过将当前 html() 与存储的 html() 进行比较来完成所有这些更改跟踪?
  3. 如果(2)是,这会处理“撤消”的情况吗?

编辑:我有一个 ajax 上传内容并保存更改的按钮。然后我删除边框颜色

4

1 回答 1

0

1.) You could track HTML changes in a similar way that the jQuery livequery plugin does. The livequery plugin wraps all of the jQuery DOM manipulation methods and when any of them are called, the wrapper method does something special and then proxies back to the original function. This will only work for the update/undo uses cases assuming the both use one of the jQuery DOM manipulation methods to change state.

$.each('append', 'prepend', 'after', 'before', 'wrap', 'attr', 'removeAttr', 'addClass', 'removeClass', 'toggleClass', 'empty', 'remove', 'html', function(i, funcName) {

   // Short-circuit if the method doesn't exist
   if (!$.fn[funcName]) return;

   // Save a reference to the original method
   var old = $.fn[funcName];

   // Create a new method
   $.fn[funcName] = function() {

      // Call the original method
      var r = old.apply(this, arguments);

      //Do something special here! Compare the stored html of the fieldset
      //to the new html state and update border accordingly

      // Return the original methods result
      return r;
   }
});

2.) You could keep track this way, seems a little heavy handed. Without more information about your use case and control of data it is hard to recommend anything.

3.) If you did store the value of the original html() in the fieldset it seems that it would work for the undo case as well. You could just compare the value of the html() after an undo as well. However if you are creating an "undo" button it seems to me that you will need to have some history of all changes, and once the user has no more undos then they should be back at the original state and no comparison of the html() should be needed.

于 2011-03-06T03:20:59.757 回答