简体   繁体   English

为什么使用javascript时带html标签的文本无法正确显示?

[英]Why does my text with html tags doesn't show right when I use javascript?

I have this text : 我有这段文字:

 <p class="rtejustify">No &acirc;mbito do Programa de Literacia Estat&iacute;stica promovido pela Biblioteca, a terceira sess&atilde;o decorrer&aacute; no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estat&iacute;stico o Portal do Eurostat.</p>

that if i use it like this: 如果我这样使用它:

<div class="corpo">     
    <p class="rtejustify">No &acirc;mbito do Programa de Literacia Estat&iacute;stica promovido pela Biblioteca, a terceira sess&atilde;o decorrer&aacute; no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estat&iacute;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 &acirc;mbito do Programa de Literacia Estat&iacute;stica promovido pela Biblioteca, a terceira sess&atilde;o decorrer&aacute; no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estat&iacute;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 &acirc;mbito do Programa de Literacia Estat&iacute;stica promovido pela Biblioteca, a terceira sess&atilde;o decorrer&aacute; no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estat&iacute;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 . 奇怪的是,我的浏览器对texthtml具有相同的行为,但是基本上,如果您使用的是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 &acirc;mbito do Programa de Literacia Estat&iacute;stica promovido pela Biblioteca, a terceira sess&atilde;o decorrer&aacute; no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estat&iacute;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应该放在单引号之间。

method 1 方法1

insted use javascript direct function like this: document.getElemenstByClassName('corpo')[0].innerHTML="your text" 插入使用javascript这样的直接函数: document.getElemenstByClassName('corpo')[0].innerHTML="your text"

method 2 方法2

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"))这一点与上面的那一点不同,它打印所有标签和所有内容。

method 3 方法3

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. 您可以在控制台中看到自己的情况。

See working example 参见工作示例

Replace your code with the below code. 用下面的代码替换您的代码。

var texto= "<p class='rtejustify'>No &acirc;mbito do Programa de Literacia Estat&iacute;stica promovido pela Biblioteca, a terceira sess&atilde;o decorrer&aacute; no dia 28, entre as 14h30 e as 17h00, na Sala EC-101 (STI), tendo como recurso estat&iacute;stico o Portal do Eurostat.</p>";
$(".corpo").text(texto);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在html文件的head标签之间引用外部文件时,为什么JavaScript不运行? - Why isn't JavaScript running when I refernce the external file in between the head tags in my html file? 加载html页面时,为什么我的外部javascript文件不起作用? - Why doesn't my external javascript file work when I load my html page? 为什么我在 javascript 和 html 中使用 getElementById 方法时显示标签的类型 - Why does it show the tag's type when I use getElementById method in javascript and html 当我使用Javascript innerHTML调用它时,为什么我的图像不显示? - Why does my image not show up when I use Javascript innerHTML to call it? 为什么html标记不起作用并显示为字符串值? - why html tags does't work and show as string value? 为什么我们不能直接在javascript中使用html输入标签? - Why can't we use html input tags in javascript directly? 为什么ng-if弄乱了我的JavaScript而ng-show没有弄乱我的JavaScript? - Why does ng-if messes up with my javascript while ng-show doesn't? 我不知道为什么当我在我的 HTML 程序上单击“Click = Past”按钮时,绿色字符图像不显示我在可汗学院尝试过 - I don't know why When I click the button "Click = Past" on my HTML program the green character image doesn't show up I tried to this in Khan academy 为什么在我点击右侧的“箭头”后,我的页面不再显示我的“.wall”(.wall 是额外信息)[netflix 滑块] - Why doesn't my page show me my '.wall' anymore after i clicked on the right "arrow"(.wall is extra info) [netflix sliders] 为什么在我的功能中有警报时我的视图会更新,而当我没有警报时却没有更新 - Why does my view update when I have an alert in my function but doesn't when I do not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM