2016-12-05 104 views
0

我正在使用ckeditor;目前這是一個錯誤,使得編輯器用<font>I made a post on it here)代替<span>標籤。我提交了一個錯誤報告來解釋這個問題,但不能等待修復,所以現在我想實現我自己的。我想知道如何可靠地將<font>標籤無縫地轉換爲<span>標籤。例如,如果我有以下幾點:字體跨度轉換器

<font face="Raleway" size="18" color="blue" class="makers styles">Simple Text</font> 

跨度相當於將是這樣:

<span style="font-family:Raleway; size:18px; color:blue;" class="makers styles">Simple Text</span> 
+0

大概的想法,複製並粘貼到一個文本編輯器,允許正則表達式查找和替換,並構建一個正則表達式字符串,將所有屬性轉換爲內聯樣式。我可能會建議使用regex101.com來測試和構建正則表達式。 – MCMXCII

回答

2

像這樣的東西應該工作:

$(function() { 

    // Your WYSIWYG content string 
    var content = ""; 

    $('font', content).each(function() { 

     // Append a new <span> after this <font> 
     $(this).after(function() { 
      var span = $('<span>').text($(this).text()); 

      if(this.hasAttribute('face')) { 
       span.css('font-family', $(this).attr('face')); 
      } 
      if(this.hasAttribute('size')) { 
       span.css('font-size', $(this).attr('size') + 'px'); 
      } 
      if(this.hasAttribute('color')) { 
       span.css('color', $(this).attr('color')); 
      } 
      if(this.hasAttribute('class')) { 
       span.attr('class', $(this).attr('class')); 
      } 

      return span; 
     }); 

     // Remove this <font> 
     $(this).remove(); 

    }); 

});