【发布时间】: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 类,以便我可以使 heading1 和 collapse1 随着每个新添加相应地增加。另外我想我需要能够编辑已经存在的那些,以防删除一个,这样添加的下一个就不是另一个的副本。
有没有办法在 jQuery 或 javascript 中计算这些?
谢谢, 詹姆斯
【问题讨论】:
标签: tinymce countelements