[英]replace() tag did not work in javascript
我有數據庫,當前在表中使用內聯編輯第一個問題是格式化文本的粘貼,但是看來,此腳本已將問題固定為90%:
<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>
該腳本會刪除標簽,但保留
,這導致我的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(" "," ");
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(" "," ");
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(" "," ");
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;
}
}
但是什么都沒有
該腳本會刪除標簽,但保留
,這導致我的sql ajax失敗
這是因為您忽略了對輸入到查詢字符串中的參數值進行正確的URL編碼。
對之前的值使用encodeURIComponent
。
最后的問題是如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");
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.