2017-06-02 70 views
0

我有數據庫,目前使用中的表 第一個問題是與格式化文本的粘貼,但似乎聯編輯,這一問題被固定在90%,與此腳本:REPLACE()標籤不工作在JavaScript

<script type="text/javascript"> 

       var _onPaste_StripFormatting_IEPaste = false; 

       function OnPaste_StripFormatting(elem, e) { 

        if (e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) { 
         e.preventDefault(); 
         var text = e.originalEvent.clipboardData.getData('text/plain'); 
         window.document.execCommand('insertText', false, text); 
        } 
        else if (e.clipboardData && e.clipboardData.getData) { 
         e.preventDefault(); 
         var text = e.clipboardData.getData('text/plain'); 
         window.document.execCommand('insertText', false, text); 
        } 
        else if (window.clipboardData && window.clipboardData.getData) { 
         // Stop stack overflow 
         if (!_onPaste_StripFormatting_IEPaste) { 
          _onPaste_StripFormatting_IEPaste = true; 
          e.preventDefault(); 
          window.document.execCommand('ms-pasteTextOnly', false); 
         } 
         _onPaste_StripFormatting_IEPaste = false; 
        } 

       } 

    </script> 

我的PHP代碼如下所示:

<td contenteditable='true' onblur=saveToDatabase(this,'titleeng','".$data['id']."') onClick='showEdit(this);' onpaste='OnPaste_StripFormatting(this, event);'>".$data['titleeng']."</td> 

腳本刪除標籤,但保留&nbsp;,這導致我的SQL AJAX失敗

這是Ajax的腳本:

<script> 
     function showEdit(editableObj) { 
      $(editableObj).css("background","#FFF"); 
     } 

     function saveToDatabase(editableObj,column,id) { 
      $(editableObj).css("background","#12ff65 url(loaderIcon.gif) no-repeat right"); 
      $.ajax({ 
       url: "saveedit.php", 
       type: "POST", 
       data:"column="+column+"&editval="+editableObj.innerHTML+"&id="+id, 
       success: function(data){ 
        $(editableObj).css("background","#FDFDFD"); 
       }   
      }); 
     } 
     </script> 

我想這一點: 新組件text = text.replace("&nbsp"," ");

var _onPaste_StripFormatting_IEPaste = false; 

       function OnPaste_StripFormatting(elem, e) { 

        if (e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData) { 
         e.preventDefault(); 
         var text = e.originalEvent.clipboardData.getData('text/plain'); 
         string text = text; 
         text = text.replace("&nbsp"," "); 
         window.document.execCommand('insertText', false, text); 
        } 
        else if (e.clipboardData && e.clipboardData.getData) { 
         e.preventDefault(); 
         var text = e.clipboardData.getData('text/plain'); 
         string text = text; 
         text = text.replace("&nbsp"," "); 
         window.document.execCommand('insertText', false, text); 
        } 
        else if (window.clipboardData && window.clipboardData.getData) { 
         // Stop stack overflow 
         if (!_onPaste_StripFormatting_IEPaste) { 
          _onPaste_StripFormatting_IEPaste = true; 
          e.preventDefault(); 
          window.document.execCommand('ms-pasteTextOnly', false); 
         } 
         _onPaste_StripFormatting_IEPaste = false; 
        } 

       } 

但沒有

+0

https://jsfiddle.net/subsa/79dr2grn/未替換,甚至第一羣組。從CSS粘貼文本,有隱藏nbsp –

回答

1

腳本刪除標籤,但保留&nbsp;,即導致我的sql阿賈克斯失敗

這是因爲您忽略了對插入到查詢字符串中的參數值進行了URL編碼。

以前的值使用encodeURIComponent

+0

我試了一下,但是在保存顯示之後返回值爲%3%2等,除了休息消失 –

+0

_「但是返回值與%3%2等」 _ - 是的,這就是URL編碼_is_的百分比。 _「保存之後顯示正確,除了中斷消失」 - 這與URL編碼沒有任何關係,但更像是輸出數據的問題。但是如果你想要幫助解決這個問題,你需要更具體地說明你在這裏的意思。 – CBroe

+0

@pete:不,URL編碼不必顛倒 - 在您訪問PHP中的參數值時已經發生。 – CBroe

0

最後的問題是與編碼爲CBroe說

這裏是AJAX技術,使得它的工作

function saveToDatabase(editableObj,column,id) { 
      $(editableObj).css("background","#12ff65 url(loaderIcon.gif) no-repeat right"); 
      var editxx = encodeURIComponent(editableObj.innerHTML); 
      $.ajax({ 
       url: "saveedit.php", 
       type: "POST", 
       data:"column="+column+"&editval="+editxx+"&id="+id, 
       success: function(data){ 
        $(editableObj).css("background","#FDFDFD"); 
       }   
      }); 
     }