开发者

how to check classes of node inside TinyMCE?

开发者 https://www.devze.com 2023-01-07 16:52 出处:网络
I want to check if the node inside the TinyMCE has a class starting with word \'border\'. I tried to retrieve classes of the node, using

I want to check if the node inside the TinyMCE has a class starting with word 'border'. I tried to retrieve classes of the node, using

 tinyMCE.activeEditor.dom.getClasses();

but it returned null.

Is there any way to do this ? I need to find if the node has any class sta开发者_运维知识库ring with word 'border', if it has then replace that class with other.


No problem. Assuming you have only one node inside your editor it is:

class_string = tinyMCE.activeEditor.getBody().firstChild.class;

class_string_array = class_string.split(" ");

for (i=0; i < class_string_array.length; i++){
   if (class_string_array[i].search("border") !== -1) {
      alert("class found!");
      class_string_array[i] = "new_class";

      class_string = class_string_array.join(" ");
      tinyMCE.activeEditor.getBody().firstChild.setAttribute('class',class_string);

      break;      
   }
}

It is much easier to use jQuery:

node = tinyMCE.activeEditor.getBody().firstChild;
if ( $(node).hasClass("border_xxx") ){ // need to check for explicit className
   $(node).removeClass("border_xxx");
   $(node).addClass("new_class");
} 

EDIT: If you want to check for every node inside the content you will need to go through the body-domtree recursivly and check for each node:

function getTextNodesValues(tinymce,node) {
    if ( $(node).hasClass("border_xxx") ){ // need to check explicit className
       $(node).removeClass("border_xxx");
       $(node).addClass("new_class");
    } 

    for (var i=0; i<node.childNodes.length; i++) {
      el = node.childNodes[i];
      if ( $(node).hasClass("border_xxx") ){ // need to check explicit className
         $(node).removeClass("border_xxx");
         $(node).addClass("new_class");
      } 
      if (el.childNodes.length > 0) getTextNodesValues(tinymce,el);
    }
}

getTextNodesValues(tinymce, getTextNodesValues(tinymce,node) );

Should get you all Classes inside the Editor:

 tinyMCE.activeEditor.dom.getClasses();

but it is not easy to replace them with that info.

0

精彩评论

暂无评论...
验证码 换一张
取 消