1

私はjqueryプラグインをコーディングしています.2つの状態(編集/保存など)を持ついくつかのボタンが必要です.JSONを介してこの情報を取得し、ボタンに次のように挿入します:

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

ボタンをクリックすると、ここに移動してアクションを変更します。したがって、テンプレートを介してリンク全体と、button.data から取得した情報を置き換えたいと考えています。innerHtml だけでなく、outer も置き換えたいので、「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
 }

問題は、関数から出ると、それが未定義であると言うように、新しいデータを失うことです。誰か助けてくれませんか?「replaceWith」を使用することは必須ではないため、別の解決策を思いついた場合は問題ありません。

4

1 に答える 1

0

わかりました、解決しました。

Diode のおかげで、jsfiddle で再現してみました。クリック機能も機能しなかったので、コードを少し変更しました。テキストに置き換える代わりに:

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

オブジェクトでそれを行います:

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

実際の動作を確認できます: http://jsfiddle.net/p8vMR/9/

于 2012-03-01T12:36:14.773 に答える