[英]Why does my text with html tags doesn't show right when I use javascript?
I have this text : 我有这段文字:
<p class="rtejustify">No âmbito do Programa de Literacia Estatística promovido pela Biblioteca, a terceira sessão decorrerá no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estatístico o Portal do Eurostat.</p>
that if i use it like this: 如果我这样使用它:
<div class="corpo">
<p class="rtejustify">No âmbito do Programa de Literacia Estatística promovido pela Biblioteca, a terceira sessão decorrerá no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estatístico o Portal do Eurostat.</p>
shows up correct in the html page... Like this : 在html页面中显示正确...像这样:
No âmbito do Programa de Literacia Estatística promovido , a terceira sessão decorrerá no tendo como recurso estatístico o Portal do Eurostat. 不能在“Estatísticapromovido计划书”或“ Eurostat门户网站”上使用“téceirasessãodecorrerá”。
but i if get the same code in a js function and put it on a variable, when i open the html file the html tags are also presented... 但是我如果在js函数中获得相同的代码并将其放在变量中,则当我打开html文件时,还会显示html标签...
Example: click to test 示例: 点击测试
var texto= "<p class="rtejustify">No âmbito do Programa de Literacia Estatística promovido pela Biblioteca, a terceira sessão decorrerá no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estatístico o Portal do Eurostat.</p>"; $(".corpo").text(texto);
.corpo { background-color:#000066 ; opacity: 0.8; filter: alpha(opacity=50); width:1000px; height:380px; position:fixed; z-index:3; overflow: scroll; color:white; font-family:Arial; font-size:20px; font-style:normal; font-weight:bold; overflow-y:scroll; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="corpo"> <p class="rtejustify">No âmbito do Programa de Literacia Estatística promovido pela Biblioteca, a terceira sessão decorrerá no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estatístico o Portal do Eurostat.</p> </div>
Instead of the text
function, use the html
function. 代替text
函数,使用html
函数。 http://api.jquery.com/html/ http://api.jquery.com/html/
It is strange that my browser has the same behaviour for text
and html
, but basically, if you're using html: use html
, otherwise use text
. 奇怪的是,我的浏览器对text
和html
具有相同的行为,但是基本上,如果您使用的是html:请使用html
,否则请使用text
。
Using Opera on Mac OS X Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 OPR/29.0.1795.47
It is maybe a bug in Opera or by (flawed) design. 在Mac OS X上使用Opera Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 OPR/29.0.1795.47
这可能是Opera或(有缺陷的)设计。
var texto= "<p class='rtejustify'>No âmbito do Programa de Literacia Estatística promovido pela Biblioteca, a terceira sessão decorrerá no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estatístico o Portal do Eurostat.</p>";
$(".corpo").html(texto);
Try this code.. 试试这个代码。
@Barmar : You should have used html() instead of text(). @Barmar:您应该使用html()而不是text()。 And You have used doublequotes inside the javascript variable texto . 并且您已经在javascript变量texto中使用了双引号。 The classname rtejustify should be in placed between singlequotes. 类名rtejustify应该放在单引号之间。
insted use javascript direct function like this: document.getElemenstByClassName('corpo')[0].innerHTML="your text"
插入使用javascript这样的直接函数: document.getElemenstByClassName('corpo')[0].innerHTML="your text"
document.getElemenstByClassName('corpo')[0].appendChild(document.createTextNode("your text"))
this one is different from the above one in the aspect that it prints all the tag and everything. document.getElemenstByClassName('corpo')[0].appendChild(document.createTextNode("your text"))
这一点与上面的那一点不同,它打印所有标签和所有内容。
document.getElemenstByClassName('corpo')[0].innerHTML= document.getElemenstByClassName('corpo')[0].value+ "your text"
this way allows us to use the tags. 这种方式使我们可以使用标签。
method 2 and method 3 is like usign Jquery $(".corpo").text(texto);
方法2和方法3类似于usign Jquery $(".corpo").text(texto);
and $(".corpo").html(texto);
和$(".corpo").html(texto);
respectively.(please not that i don't think it is the implementation of Jquery). 分别。(请不要说我不认为这是Jquery的实现)。
The difference lies between these two method is that the Ist creates a Node what that mean Javascript treates everything as a String it dont differentiates between <p>
and p
ie tags and simple text where as innerHTML
just put the things in between the double quotes in an element becauses of this we can take the advantages of tags there. 这两种方法之间的区别在于,Ist创建了一个Node,这意味着Javascript将所有内容都视为String,它不会在<p>
和p
之间进行区分,即标记和简单文本,而innerHTML
只是将其放在双引号之间因此,我们可以在其中利用标签的优势。
This technique is used in Firefox. Firefox使用了此技术。 There in textarea
everyline you enter it is put into the textarea
tag as a text Node. 在textarea
您输入的每一行都将其作为文本节点放入textarea
标记中。 You can see this your self in Console. 您可以在控制台中看到自己的情况。
Replace your code with the below code. 用下面的代码替换您的代码。
var texto= "<p class='rtejustify'>No âmbito do Programa de Literacia Estatística promovido pela Biblioteca, a terceira sessão decorrerá no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estatístico o Portal do Eurostat.</p>";
$(".corpo").text(texto);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.