如何删除html元素,同时保持输入值内的文本不变?

时间:2019-04-14 11:37:14

标签: html css jscript

需要从输入的值标签中删除标签。

该元素是由主机的CMS生成的,因此,我可以删除粗体标签的唯一方法是运行脚本。

<span id="kkkje30">
<input type="text" id="siF14" class="manFlPassw" value="<b>generated text</b>" maxlength="15">
</span>

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式来删除输入值中的标记,例如

$(document).ready(function(){
  $('input').each(function(index, item){
    var regex = /(<([^>]+)>)/ig;
    var value = $(item).val();
    var removedtag = value.replace(regex, '');
    $(this).val(removedtag);
  });
})

演示:

$(document).ready(function(){
  $('input').each(function(index, item){
    var regex = /(<([^>]+)>)/ig;
    var value = $(item).val();
    var removedtag = value.replace(regex, '');
    $(this).val(removedtag);
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="kkkje30">
<input type="text" id="siF14" class="manFlPassw" value="<b>generated text</b>" maxlength="15">
</span>