1

有没有办法测量 tinymce 编辑器实例中的元素?我需要测量一个表格的宽度和它的列,以便我可以找到表格的相对列宽,即使表格里面有内容,下面的代码也会返回 0。

var doc = tinymce.get('text_box_desc0').getDoc();
var width = $('table', doc).width();
alert(width); 
4

1 回答 1

1

是的,这是可能的。

这是一个如何使用getBoundingClientRect()来获取一些信息的示例

        var ed = tinymce.get('text_box_desc0');

        // this will take the first table in the editor, you may define another selector to get 'your' table element 
        var elem = $(ed.getBody()).find('table:first').get(0);
        try {
            box = elem.getBoundingClientRect();
        } 
        catch(e) 
        {
            console.log('error creating box: ' + e);
        }
        // various info
        var doc = ed.getDoc(),
            docElem = doc.documentElement,
            body = ed.getBody(),
            win = ed.getWin(),
            clientTop  = docElem.clientTop  || body.clientTop  || 0,
            clientLeft = docElem.clientLeft || body.clientLeft || 0,
            scrollTop  = win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop,
            scrollLeft = win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft,
            top  = box.top  + scrollTop  - clientTop,
            left = box.left + scrollLeft - clientLeft;

你正在寻找的是

box.width;

但还有更多的属性,比如

box.height; 
于 2012-11-19T15:32:44.510 回答