【问题标题】:tinymce: how many elements with class are in texttinymce:文本中有多少具有类的元素
【发布时间】:2015-11-30 15:01:57
【问题描述】:

我想添加一个自定义按钮来设置引导手风琴元素。为此,我需要知道文本中有多少其他手风琴元素,以便我可以为新元素提供正确的 ID。现在我有:

    // Add a custom button
    ed.addButton('accordianArea', {
        title : 'Accordian Area',
        text : '[accordian/]',
        icon: false,
        onclick : function() {
            // Add you own code to execute something on click
            ed.focus();

            var text = ed.selection.getContent({'format': 'html'});

            var accordianText = '<div class="panel panel-default">' +
                '<div class="panel-heading" role="tab" id="heading1">' +
                    '<h4 class="panel-title">' +
                        '<a role="button" data-toggle="collapse" href="#heading1" aria-expanded="true" aria-controls="heading1">' +
                            'Accordian Title<span id="_cursor" />' +
                        '</a>' +
                    '</h4>' +
                '</div>' +
                '<div id="heading1" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading1">' +
                    '<div class="panel-body">' +
                        (text ? text : 'Accordian Text') +
                    '</div>' +
                '</div>' +
            '</div>';

            ed.execCommand('mceInsertContent', false, accordianText);

            ed.selection.select(ed.dom.select('#_cursor')[0]); //select the inserted element
            ed.selection.collapse(0); //collapses the selection to the end of the range, so the cursor is after the inserted element
            ed.dom.remove('_cursor'); //remove the element
        }
    });

我需要能够计算文本中有多少 panel-collapse 类,以便我可以使 heading1collapse1 随着每个新添加相应地增加。另外我想我需要能够编辑已经存在的那些,以防删除一个,这样添加的下一个就不是另一个的副本。

有没有办法在 jQuery 或 javascript 中计算这些?

谢谢, 詹姆斯

【问题讨论】:

    标签: tinymce countelements


    【解决方案1】:

    是的,这是可能的:

    var ed = tinymce.get('your_editor_id');
    var $elements = $(ed.getBody()).find('.panel-collapse');
    var count = $elements.length;
    

    【讨论】:

    • 这对于获取原始计数非常有用。那么,我想我也可以使用.each(function() {}) 来重命名元素,以防元素#2 of 4 被删除并添加另一个元素?这样它就会重新排序当前元素,因为新元素现在是元素#4?
    • 我刚刚测试了我自己的评论,它确实有效。再次感谢。
    • 很高兴能够提供帮助
    猜你喜欢
    • 2021-12-20
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多