如何替换 用 < 和 > with > 使用 jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6784560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Replace < with < and > with > using jquery
提问by sebas
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 <
and >
into HTML encoded characters.
我有一个页面,它是后端 CRM 管理面板的一部分。在该页面上,HTML 输出来自一些我无法访问的 PHP 函数。并且该 HTML 会自动更改<
并>
转换为 HTML 编码的字符。
So there is a div that contains html tags like <br />
that is converted into <b />
所以有一个包含 html 标签的 div<br />
被转换成<b />
So I need to change it back to the HTML characters using only jQuery:
所以我需要只使用 jQuery 将它改回 HTML 字符:
<
to <
>
to >
<
到<
>
到>
Is there a jQuery script I can use to replace those special characters with the corresponding symbols? This will mean my HTML tags will actually work and the HTML will being displayed properly on the screen?
是否有一个 jQuery 脚本可以用来将这些特殊字符替换为相应的符号?这是否意味着我的 HTML 标签实际上可以工作并且 HTML 将正确显示在屏幕上?
I've tried removewith()
but i can't make it work.
我试过了,removewith()
但我不能让它工作。
ADDED: The div that im trying to modify is this
添加:我试图修改的 div 是这个
<div style="font-size: 11px; width: 90%; font-family: Tahoma;" id="cotiz"><strong>Valuación</strong> de InfoAuto: 35.500,00<br />
Cotización Seleccionada: Ninguna<br />
Allianz, Responsabilidad Civil: 5,25<br />
Allianz, Terceros Completos: 8,85 </div>
采纳答案by genesis
The simplest thing to do would be
最简单的事情是
$('#test').each(function(){
var $this = $(this);
var t = $this.text();
$this.html(t.replace('<','<').replace('>', '>'));
});
回答by Kiran Banda
Please try this
请试试这个
.replace(/</g, '<').replace(/>/g, '>')
to replace these characters globally. I tried this and works like a charm :)
全局替换这些字符。我试过了,效果很好:)
回答by AdiechaHK
I have different solution then the conventional, and it will be applied to decode/encode html
我有与传统不同的解决方案,它将应用于解码/编码 html
Decode
解码
var encodedString = "<Hello>";
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 "<Hello>" this string
回答by Matt Ball
$('#myDivId').text(function (i, text)
{
return text.replace('<', '<').replace('>', '>');
});
回答by Are.exe
if use underscore.js exist _.unescape(string)
如果使用 underscore.js 存在 _.unescape(string)
回答by Shashikant Pandit
Try This:-
尝试这个:-
var wrapper=$(".contentwrap").html();
wrapper=wrapper.replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
$(".contentwrap").html(wrapper);
回答by Ballon
Use $this.html('...'); instead $this.text('...');
使用 $this.html('...'); 相反 $this.text('...');
回答by Pallav Nagar
You can do it simply with php
你可以简单地用php来做
<?php
$a =
'<div style="font-size: 11px; width: 90%; font-family: Tahoma;" id="cotiz"><strong>Valuación</strong> de InfoAuto: 35.500,00<br />
Cotización Seleccionada: Ninguna<br />
Allianz, Responsabilidad Civil: 5,25<br />
Allianz, Terceros Completos: 8,85 </div>';
$b = html_entity_decode($a);
echo $b;
?>
回答by Yassine Younes
All of the above didn't really work for me because what I needed was something to replace all <
to < and >
to > , not only the first one.
What I did was:
以上所有对我来说都没有真正起作用,因为我需要的是将所有内容替换<
为 < 和>
> 的东西,而不仅仅是第一个。我所做的是:
.split('<').join('<').split('>').join('>');
Just thinking out of the box here. It worked for me, I hope it does for you too.
只是在这里开箱即用。它对我有用,我希望它对你也有用。
回答by Evan
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!
我需要逐步找到 H1,然后解析下一个元素,因为我无法像您一样使用特定的 ID。好提示!
$('#sscContent').find("h1").next().each(function(){
var $this = $(this);
var t = $this.text();
$this.html(t.replace('<','<').replace('>', '>'));
});