【问题标题】:javascript: deleting tags of a selected element in a textjavascript:删除文本中选定元素的标签
【发布时间】:2016-04-05 14:54:28
【问题描述】:

我在我的 textarea 中为选定的元素添加了一个标签,每个标签都有一个 id。我将元素存储在一个表格中。 但是,我想删除一些标签(我必须输入标签“ref”和“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 的初学者

【问题讨论】:

  • 小提琴会很有帮助
  • 我使用它,但有时它没有给我解决方案

标签: javascript html text tags


【解决方案1】:
if(modif.match('[^<]'+tag+'id="(\\d+)">') && modif.match('</'+tag+'[>$]'))

也许你在正则表达式中需要一个额外的空间:

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

【讨论】:

  • 不,它不起作用。另外,我的 id 有问题,我应该减少在要删除的标签之后创建的所有标签的 id。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多