在ckeditor中查找并替换html(不仅仅是文本)?

时间:2013-02-11 12:42:26

标签: javascript jquery asp.net ckeditor

我在申请中使用CKEDITOR 4.

这里我有一种特定类型的标签,比方说'TagX'。

我只需要为那些没有id的TagX标签添加'id'属性。

我已经使用了:

content = EditorInstance.getData();
var pattRegex = /<(tagx)(?![^<>]*\bid=).*?<\/\1>/gi;
var arrTagsX = content.match(pattRegex);
for (i = 0; i < arrTagsX.length; i++) {
    content = content.replace("<tagx", <tagx id=\"id_" + i + "\"");
}
EditorInstance.setData(content);

它的工作正常,但内容大小非常大,所以我想避免使用setData();

我想要一些东西找到html(不仅是文本)并用html + id替换html。

2 个答案:

答案 0 :(得分:3)

为什么不使用$('tagx'),然后在显示时使用$(this)来照顾您的员工?

因此,让我们通过以下

之类的方式为jquery提供工作
var i=0;
$('tagx').each(function(){
   i++;
   $(this).attr('id','id_'+i);
});

根据您的其他评论进行修改

让我们测试一下:not([id])

var i=0;
$('tagx:not([id])').each(function(){
       i++;
       $(this).attr('id','id_'+i);
});

答案 1 :(得分:1)

最后我按照下面的说法更新了我的代码,并且满足了我的要求......

rootElement = EditorInstance.document.getElementsByTag("rootEle").getItem(0);
var tagxEle = rootElement.$.getElementsByTagName('tagx');
var i=0;
 $(tagxEle).not('[id]').each(function(){
    i++;                                        
    $(this).attr('id','tagx_'+i);
 });

thnx Valky的答案......