【问题标题】:Remove a tag and content of it by substring and indexof in javascript通过 javascript 中的 substring 和 indexof 删除标签及其内容
【发布时间】:2013-05-05 06:22:51
【问题描述】:

我想转换这个:

<html>
s
<RB:Block_Left>sss</RB:Block_Left>
s
hello
</html>

到这里,

<html>
s
s
hello
</html>

而且这段代码必须在文本区域中,我做到了,但不能工作!

<textarea id ="code">
<html>
s
<RB:Block_Left>sss</RB:Block_Left>
s
hello
</html>
</textarea>
<br />
<input type="button" value="makeit!" onclick="ARAS()" />
<br />
<textarea id ="newcode">
</textarea>
<script>
function ARAS(){
    str=document.getElementById('code').value;
    str=str.substring(  + str.indexOf('<html>') - str.indexOf('<\/RB:Block_Left>') + str.indexOf('<\/RB:Block_Left>'));
    document.getElementById('newcode').value=str;
}
</script>

【问题讨论】:

    标签: javascript substring indexof


    【解决方案1】:

    换行:

    str=str.substring(  + str.indexOf('<html>') - str.indexOf('<\/RB:Block_Left>') + str.indexOf('<\/RB:Block_Left>'));
    

    用这一行:

    str=str.substring(0,str.indexOf("<RB")-1) + str.substring(str.indexOf("/RB")+15)
    

    【讨论】:

      【解决方案2】:

      您可以像处理 HTML 元素一样使用 jQuery:

      var ta = $("#code"); // Textarea
      var tag = 'tag'; // Tag name or selector you want to remove
      
      ta.val(
        $("<div>" + ta.val() + "</div>") // Wrap content of the textarea into a DIV tag
          .find(tag) // Find all the tags you want to remove
          .remove() // Remove them
          .end() // Get back the parent element
          .html() // Get HTML of the element
      );
      

      http://jsfiddle.net/w7Rkm/

      【讨论】:

        猜你喜欢
        • 2016-06-03
        • 1970-01-01
        • 2022-11-21
        • 1970-01-01
        • 2020-11-11
        • 1970-01-01
        • 2018-10-28
        • 1970-01-01
        • 2011-10-10
        相关资源
        最近更新 更多