1

我正在编写一个 jquery 插件,我需要一些按钮来获得双重状态(如编辑/保存)我通过 JSON 获取此信息并将其插入按钮中:

node
    - current //['primary'/'secondary']
    - primary // some info
    - secondary // some info

单击按钮后,我将在此处更改操作。所以我想通过一个模板和我从 button.data 获得的信息来替换整个链接。由于我不仅要替换 innerHtml 还要替换外部,所以我必须使用' replaceWith '。然后我将“数据”复制到新按钮并(理想情况下)删除旧按钮。

changeButtonAction : function(button, selector){
      var node = button.data('node'),
           info;

      if(node.current == 'primary'){
           info = node.secondary;
           node.current = 'secondary';
      }else{
           info = node.primary;
           node.current = 'primary';
      }

      button.replaceWith(multiReplace(OPERATION_ITEM, info, true));
      button.data('node', $.extend( true, {}, node));

      ... //bit of interaction
 }

问题是:一旦退出函数,我就会丢失新数据,因为它说它是undefined。有人可以帮忙吗?使用“replaceWith”不是必须的,所以如果你想出另一个解决方案就可以了。

4

1 回答 1

0

Ok, I solved it.

Thanks to Diode I tried reproducing it in jsfiddle. The click function did not work neither so I changes my code a bit. Instead of replacing with text:

button.replaceWith(multiReplace(OPERATION_ITEM, info, true));
button.data('node', $.extend( true, {}, node));

Do it with an object:

var button2 = $(multiReplace(OPERATION_ITEM, info, true))
    .data('node', $.extend( true, {}, node));
button.replaceWith(button2);

You can see it in action: http://jsfiddle.net/p8vMR/9/

于 2012-03-01T12:36:14.773 回答