How To Replace < with> with > using jquery

前端 未结 10 752
鱼传尺愫
鱼传尺愫 2020-12-13 20:14

I have a page that is part of a backend CRM admin panel. On that page the HTML output comes from some PHP functions that I can\'t access. And that HTML automatically changes

相关标签:
10条回答
  • 2020-12-13 20:51
    $('#myDivId').text(function (i, text)
    {
        return text.replace('&lt;', '<').replace('&gt;', '>');
    });
    
    0 讨论(0)
  • 2020-12-13 20:55

    All of the above didn't really work for me because what I needed was something to replace all &lt; to < and &gt; to > , not only the first one. What I did was:

    .split('&lt;').join('<').split('&gt;').join('>');
    

    Just thinking out of the box here. It worked for me, I hope it does for you too.

    0 讨论(0)
  • 2020-12-13 21:00

    Try This:-

    var wrapper=$(".contentwrap").html();
      wrapper=wrapper.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
     $(".contentwrap").html(wrapper);
    
    0 讨论(0)
  • 2020-12-13 21:02

    I have different solution then the conventional, and it will be applied to decode/encode html

    Decode

    var encodedString = "&lt;Hello&gt;";
    var decodedText = $("<p/>").html(encodedString).text(); 
    /* this decodedText will give you "<hello>" this string */
    

    Encode

    var normalString = "<Hello>";
    var enocodedText = $("<p/>").text(normalString).html();
    /* this encodedText will give you "&lt;Hello&gt;" this string
    
    0 讨论(0)
  • 2020-12-13 21:06

    Use $this.html('...'); instead $this.text('...');

    0 讨论(0)
  • 2020-12-13 21:06

    I needed to step to find an H1 then parse the next element, because I wasn't able to use a specific ID like you did. Good tip!

    $('#sscContent').find("h1").next().each(function(){
        var $this = $(this);
        var t = $this.text();
        $this.html(t.replace('&lt;','<').replace('&gt;', '>'));
    });
    
    0 讨论(0)
提交回复
热议问题