2016-04-05 29 views
0

我在我的textarea中爲選定的元素添加了標籤,每個標籤都帶有一個id。我將元素存儲在一張桌子上。 但是,我想刪除一些標籤(我必須鍵入「ref」和「point」標籤)。我嘗試這個功能,但是,它不工作:javascript:刪除文本中選定元素的標籤

function deleteTag(textField, tag) 
{ 
var scrollTop = textField.scrollTop;  //For the scroll of the text 
var start = textField.selectionStart;  //beginning of the selected text 
var end = textField.selectionEnd;   //End of the selected text 
var modif=textField.value.substring(start,end); //The text to modify 
// Remove the tag 
if(modif.match('[^<]'+tag+'id="(\\d+)">') && modif.match('</'+tag+'[>$]')) 
{ 
    var regex; 
    if(tag=="ref") 
    { 
     regex=new RegExp('<'+tag+' id="(\\d+)">'); 
     var opt=modif.match(regex)[1]; 
     document.getElementById("refList").remove(opt-1); 
    } 
    regex=new RegExp('<'+tag+'[^>]+>'); 
    modif=modif.replace(regex, ""); 
    modif=modif.replace('</'+tag+'>', ""); 
    }   
textField.value=textSup+modif+textInf; //We modify the text inside the text area 
textField.scrollTop=scrollTop; 
} 

而且按鈕有這樣的代碼:

<button onclick="deleteTag(textAreaId, 'ref')"> Effacer 

通過我在javascript

+0

小提琴將hel pful –

+0

我使用它,但有些時候它不給我一個解決方案 – AbirH

回答

0
if(modif.match('[^<]'+tag+'id="(\\d+)">') && modif.match('</'+tag+'[>$]')) 

初學者的方式也許你需要一個額外的空間在正則表達式中:

if(modif.match('[^<]'+tag+' id="(\\d+)">') && modif.match('</'+tag+'[>$]')) 
+0

不,它不起作用。此外,我有一個id的問題,我應該減少在我想要刪除的標籤之後創建的所有標籤的id。 – AbirH