一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

如何在Drupal实现Click insert textarea效果?

时间:2022-06-25 16:22:09 编辑:袖梨 来源:一聚教程网

如何在Drupal实现Click insert textarea效果?本教程讲的是单纯的实现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 = $('javascript:void(0);" title="' + Drupal.t('Insert this token into your form') + '">' + $(this).html() + '').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的效果。

热门栏目