【发布时间】: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