跳转到主要内容
Drupal猎人 提交于 3 December 2014

单纯的实现click insert textarea的效果,只需一段jquery,它在token.js里的Drupal.behaviors.tokenInsert 函数,改改寻址class就能用了,示例代码如下:

/**
 * Insert rules to textarea.
 */
Drupal.behaviors.rulesInsert = {
  attach: function (context, settings) {
    // Keep track of which textfield was last selected/focused.
    $('textarea', context).focus(function() {
      Drupal.settings.sv_rulesFocusedField = this;
    });

    $('.token-click-insert dd', context).once('token-click-insert', function() {
      var newThis = $('<a href="javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '</a>').click(function(){
        if (typeof Drupal.settings.sv_rulesFocusedField == 'undefined') {
          alert(Drupal.t('First click a text field to insert your tokens into.'));
        }
        else {
          var myField = Drupal.settings.sv_rulesFocusedField;
          var myValue = $(this).text();

          //IE support
          if (document.selection) {
            myField.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
          }

          //MOZILLA/NETSCAPE support
          else if (myField.selectionStart || myField.selectionStart == '0') {
            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd;
            myField.value = myField.value.substring(0, startPos)
                          + myValue
                          + myField.value.substring(endPos, myField.value.length);
          } else {
            myField.value += myValue;
          }

          $('html,body').animate({scrollTop: $(myField).offset().top}, 500);
        }
        return false;
      });
      $(this).html(newThis);
    });
  }
};

注:rulesInsert 可自定义,寻址class:.token-click-insert dd 需要改成你的,完整效果参看你安装token模块后插入token的效果: 20141203141642.png

Drupal 版本