【问题标题】:Change tinyMce editor's height dynamically动态改变 tinyMce 编辑器的高度
【发布时间】:2012-03-24 04:28:55
【问题描述】:

我在我的页面中使用 tinymce 编辑器。我想要做的是动态改变编辑器的高度。我创建了一个函数:

function setComposeTextareaHeight()
{
    $("#compose").height(200);
}

但这不起作用。 我的文本区域是

<textarea id="compose" cols="80" name="composeMailContent" style="width: 100%; height: 100%">

我尝试了各种改变高度的方法,但无法解决。我有什么遗漏的吗?

【问题讨论】:

  • 我想在我的脚本中做同样的事情

标签: javascript jquery tinymce


【解决方案1】:

你可以根据自己的身高来设置

tinymce.init({
     height: "500px"
});

【讨论】:

    【解决方案2】:

    我使用的是 tinymce 4.8.3。

    我在一个可调整大小的模式对话框中显示编辑器。

    我使用 flexbox 解决了这个问题,在 SASS/SCSS 中显示:

    // TinyMCE editor is inside a container something like this
    .html-container {
        height: 100%;
        overflow: hidden;
    }
    
    .mce-tinymce {
        // This prevents the bottom border being clipped
        // May work with just 100%, I may have interference with other styles
        height: calc(100% - 2px);
    
        & > .mce-container-body {
            height: 100%;
            display: flex;
            flex-direction: column;
    
            & > .mce-edit-area {
                flex: 1;
    
                // This somehow prevents minimum height of iframe in Chrome
                // If we resize too small the iframe stops shrinking.
                height: 1px; 
            }
        }
    }
    

    当编辑器初始化时,我们必须告诉它在 IFRAME 上放置 100% 的高度。在我的情况下,我还必须减去 2px,否则右边框会被剪掉:

    tinymce.init({
        ...
        height: "100%",
        width: "calc(100% - 2px)"
    });
    

    【讨论】:

      【解决方案3】:
       $(window).load(function () {
      
      $('#YourID_ifr').css('height', '550px');
      
      });
      

      【讨论】:

      • 添加有关您发布的代码的信息。您是否尝试过使用 tineMce,还是只是“尝试此代码”?
      【解决方案4】:

      有点晚了,但对于像我这样的 Google 员工,请查看autoresize plugin

      tinymce.init({
          plugins: "autoresize"
      });
      

      选项

      autoresize_min_height : 编辑器自动调整大小时的最小高度值。

      autoresize_max_height : 编辑器自动调整大小时的最大高度值。

      【讨论】:

      • 我尝试了tinymce.com/docs/plugins/autoresize 中描述的插件 Autoresize,但我找不到任何方法可以通过此插件的选项动态设置 tinyMCE 相对于浏览器大小的高度。我认为最合适的解决方案来自 Kyle Falconer。
      【解决方案5】:

      以下内容来自this other SO answer I posted

      以上方法在 TinyMCE v4 中都不适合我,所以我的解决方案是根据工具栏/菜单栏/状态栏计算高度,然后在考虑这些高度的情况下设置编辑器的高度。

      function resizeEditor(myHeight) {
          window.console.log('resizeEditor');
          myEditor = getEditor();
          if (myEditor) {
              try {
                  if (!myHeight) {            
                      var targetHeight = window.innerHeight; // Change this to the height of your wrapper element
                      var mce_bars_height = 0;
                      $('.mce-toolbar, .mce-statusbar, .mce-menubar').each(function(){
                          mce_bars_height += $(this).height();
                      });
                      window.console.log('mce bars height total: '+mce_bars_height);                          
                      myHeight = targetHeight - mce_bars_height - 8;  // the extra 8 is for margin added between the toolbars
                  }
                  window.console.log('resizeEditor: ', myHeight);
                  myEditor.theme.resizeTo('100%', myHeight);  // sets the dimensions of the editable area
              }
              catch (err) {
              }
          }
      }
      

      在我的例子中,我希望编辑器窗口与实际 window 的宽度和高度相匹配,因为编辑器会在弹出窗口中出现。为了检测更改和调整大小,我将其设置为回调:

      window.onresize = function() {
          resizeEditor();
      }
      

      【讨论】:

      • 像魅力一样工作。谢谢。
      【解决方案6】:

      如果有人发现这个并且还想更改源代码编辑器的高度插件。

      您需要编辑以下文件:

      \tiny_mce\plugins\code\plugin.min.js
      

      注意名为minHeigh 的属性并根据您的需要进行调整。您在那里定义的高度不是整个框的高度,但也不是文本区域的高度。介于两者之间。

      【讨论】:

        【解决方案7】:

        您可以使用 resizeTo 主题方法调整 tinymce 的大小:

           editorinstance.theme.resizeTo (width, height);
        

        宽度和高度设置了编辑区域的新大小——我还没有找到一种方法来推断编辑器实例的额外大小,所以你可能想做这样的事情:

           editorinstance.theme.resizeTo (new_width - 2, new_height - 32);
        

        【讨论】:

        • deduce the extra size of the editor instance - 你是说初始化后 iframe 设置为 1000+px 吗?因为这就是我所看到的。
        • 这是迄今为止最好的答案。你可以把它放在init中直接获取editorinstance,使用tinymce.activeEditor字段访问,或者使用tinymce.get(ID)
        • 展开这个。如果您只需要更改高度,请传入第一个参数(宽度)null。这只会改变高度。
        • 这在 TinyMCE v4 之后不起作用。我能够使用的解决方案是在活动编辑器中设置容器的高度:editorinstance.container.style.height = '1000px'
        • 编辑:没关系,以上仅在您启用autoresize 插件时有效。相反,您可以使用tinymce.activeEditor.editorContainer.style.height = '1000px'
        【解决方案8】:

        试试:

        tinyMCE.init({
               mode : "exact",
               elements : "elm1",
             ....
        

        在您的 javascript 代码中动态更改大小:

        var resizeHeight = 350;
        var resizeWidth = 450;
            tinyMCE.DOM.setStyle(tinyMCE.DOM.get("elm1" + '_ifr'), 'height', resizeHeight + 'px');
            tinyMCE.DOM.setStyle(tinyMCE.DOM.get("elm1" + '_ifr'), 'width', resizeWidth + 'px');
        

        【讨论】:

        • 在搜索了几篇关于这个主题的帖子之后,这是迄今为止最优雅的解决方案,也是唯一对我有用的解决方案!
        • Marc Lehmann 的答案是正确的做法,您应该更改您接受的答案。
        • 同意,Marc Lehmann 的答案是最优雅的,不使用查询选择器或依赖项,而是使用内置的 resizeTo 方法。
        【解决方案9】:

        ManseUK 所说的几乎是正确的。 正确的解决方案是:

        $('#compose_ifr').height(200);
        

        或者在你的情况下

        $('#composeMailContent_ifr').height(200);
        

        更新:也许这更符合您的要求:

            // resizes editoriframe
            resizeIframe: function(frameid) {
                var frameid = frameid ? frameid : this.editor.id+'_ifr';
                var currentfr=document.getElementById(frameid);
        
                if (currentfr && !window.opera){
                    currentfr.style.display="block";
                    if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) { //ns6 syntax
                        currentfr.height = 200 + 26;
                    }
                    else if (currentfr.Document && currentfr.Document.body.scrollHeight) { //ie5+ syntax
                            currentfr.height = 200;
                    }
                    styles = currentfr.getAttribute('style').split(';');
                    for (var i=0; i<styles.length; i++) {
                        if ( styles[i].search('height:') ==1 ){
                            styles.splice(i,1);
                            break;
                        }
                    };
                    currentfr.setAttribute('style', styles.join(';'));
                }
            },
        

        【讨论】:

        • 嘿,它确实改变了 iframe 的高度,但它没有反映在 UI 中。 firebug 捕获的代码是 iframe 的高度已更改为 200px,但包含 iframe 的 tr 的高度完全没有变化它不会反映在 UI 中。 ://
        • tinyMCE 本身的这些东西不就是为了这个目的吗? autoresize 插件适用于宽度但不适用于高度!
        猜你喜欢
        • 1970-01-01
        • 2018-05-10
        • 2018-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多