javascript:删除文本中选定元素的标记

javascript: deleting tags of a selected element in a text

本文关键字:元素 删除 文本 javascript      更新时间:2023-12-31

我在文本区域中为选定的元素添加了一个标记,每个标记都有一个id。我将元素存储在一个表中。但是,我想删除一些标签(我必须键入标签"ref"et"point")。我尝试了这个功能,但它不起作用:

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 的初学者

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

也许您需要在正则表达式中增加一个空间:

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