2017-07-06 34 views
1

我奮力這裏div來找到解決使用刪除未關閉的HTML標籤 - 而粘貼的內容與CONTENTEDITABLE

div使用CONTENTEDITABLE

PHP

<div class="panel-body"> 
<div contenteditable="true" style="text-align: justify" id="text_data_1"> 
    <?php echo(html_entity_decode($text_data_from_backend))?> 
</div>     
</div> 

jQuery

var postFormData = { 
    'agent_id'    : $("#text_data_1").val(),     
    'actionmode'   : id 
    }; 
var link = 'myfile.php'; 
$.fn.getajaxresponse(link, postFormData, $.fn.this_callback); 

將數據粘貼或輸入到可以引起爭議的div中。當最終用戶粘貼來自未正確關閉標籤的網站的內容時,整個html佈局會失敗。

我使用的是div contenteditable,這樣就可以保留新的行和基本的html標籤,並且可以來回傳輸到數據庫。

有沒有一種方法可以去掉沒有正確關閉的html標籤,我相信這是讓這種方法到位的表現。請注意我使用jQuery,PHP和MySQL

+0

*當最終用戶粘貼來自網站且標籤未正確關閉的內容時,整個html佈局失敗。這就是它應該如何工作。使用[TinyMCE](https://www.tinymce.com/)來清除用戶輸入混亂。 – apokryfos

+0

^其他所見即所得的編輯器存在;) – CD001

+0

您可以在PHP – vaso123

回答

0

感謝您的回覆。

這是一個有點棘手之一,並在搜索,我發現這裏的jQuery清理不使用正則表達式

  1. 我重視的編輯打電話Trumbowyg到div庫 - http://alex-d.github.io/Trumbowyg/ 這是很容易的和冷靜。這個是輕量級的,並且需要將js文件添加到所需的頁面。
  2. 通過ajax保存,稱爲函數,其中JClean [https://code.google.com/archive/p/jquery-clean/]庫用於清理未關閉的標記。將Jclean js文件添加到所需頁面

這就是我最終做到的。

  1. 附加編輯

    $.fn.attach_event_div_contenteditable = function(){     
        var tb_buttons = [      
             ['viewHTML'], 
             ['formatting'], 
             'btnGrp-semantic', 
             ['superscript', 'subscript'], 
             ['link'], 
             'btnGrp-justify', 
             'btnGrp-lists', 
             ['horizontalRule'], 
             ['removeformat'], 
             ['fullscreen'] 
            ]; 
        $('#text_data_1').trumbowyg({ 
         removeformatPasted: true, 
         btns: tb_buttons, 
         height:"100%", 
        }); 
        $('#text_data_2').trumbowyg({ 
         removeformatPasted: true, 
         btns: tb_buttons, 
        }); 
        $('#text_data_3').trumbowyg({ 
         removeformatPasted: true, 
         btns: tb_buttons, 
        }); 
        $('#text_data_4').trumbowyg({ 
         removeformatPasted: true, 
         btns: tb_buttons, 
        });   
    } 
    
  2. 清潔元素

    $.fn.get_latest_value = function(elem){ 
    var tagname = elem.prop("tagName"); 
    var returnvalue = ""; 
    switch(tagname.toUpperCase()){ 
        case "TEXTAREA": 
         returnvalue = elem.val();break; 
        case "INPUT": 
         returnvalue = elem.val(); break;     
        default: 
         returnvalue = elem.html();break; 
    } 
    returnvalue = returnvalue.replace(/\"/g,'&quot;'); 
    returnvalue = returnvalue.replace(/\'/g,'&apos;'); 
    var temp = $("<input value='"+returnvalue+"'/>"); 
    returnvalue = $.htmlClean(temp.val(), { format: true }); 
    return returnvalue; 
    } 
    

Ajax調用

var postFormData = { 
    'agent_id'    : $.fn.get_latest_value($("#text_data_1")),     
    'actionmode'   : id 
    }; 
var link = 'myfile.php'; 
$.fn.getajaxresponse(link, postFormData, $.fn.this_callback); 

這是我能找到的最好答案,並繼續前進。希望這有助於任何想知道我如何修復它的人。