JavaScript需要发送Esc字符\\\<br/>或文本返回&lt; p&gt;到HTML /浏览器 [英] JavaScript Needs to Send Esc Character \n or Text Return &lt;p&gt; to HTML/Browser

查看:547
本文介绍了JavaScript需要发送Esc字符\\\<br/>或文本返回&lt; p&gt;到HTML /浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)在解释器中执行JavaScript时 - 我可以使用esc序列在第二行放置一些文本
注意:myHeading.textContent的值在此屏幕截图中不同于后续两个

 > myHeading.textContent +\\\
怎么样的事情?
< 约书亚树是膨胀的,Laurel Ruth Shimer
怎么样的东西? = $ 1



2)但是当我使用相同的想法测试通过从浏览器调用html ,'\\\
'是,我会认为,只是看作
虽然我已经在html中这样做 - 当我把东西放在下一行时,HTML没有做任何特别的。这当然是有道理的。

  var myHeading = document.querySelector('h1'); 
myHeading.textContent ='很高兴再次见到你,'+ storedName +'\\\

马上再写一遍。


3)如果我尝试要添加一个html标签

,该标签只是作为文本的一部分。它不是由html引擎(我不知道该调用什么)作为实际字符读取的。

  var myHeading = document。 querySelector('h1'); 
myHeading.textContent ='很高兴再次见到你,'+ storedName +'< p>
很快就写信给我。


...

我有一个(也许两个?)Stackoverflow回应关于将html放入数组,但我不认为这是一回事。

虽然 \\\
代表现代浏览器不。要在网页上表示换行,您应该使用 < br> 元素。



现在,您已经经历过,您无法使用 textContent 属性。这是因为 textContent 属性表示元素的文本



要设置元素的内部 html ,请将请使用
innerHTML code> 属性。



这样的操作符合您的需求:

  myHeading。 innerHTML = 
'很高兴再次见到你,'+ storedName +'< br>再写我很快。


1) When executing JavaScript in the interpreter - I can use an esc sequence to put some text on a second line NOTE: the value of myHeading.textContent was different in this screen shot than in the succeeding two

   > myHeading.textContent + " \n How are things?"
   < "Joshua Tree is swell, Laurel Ruth Shimer 
   How are things?" = $1

2) But when I use the same idea testing by calling html from a browser (Safari), the '\n' is, I would think, just seen as though I had done this within html - HTML doesn't do anything special when I put something on the next line. This makes sense, of course.

   var myHeading = document.querySelector('h1');
   myHeading.textContent = 'Good to see you again, ' + storedName +  ' \n    
  Write me again soon.' ;

3) If I attempt to add an html tag,

, that tag is just included as part of the text. It's not read by the html engine (I don't know what to call that) as actual characters

   var myHeading = document.querySelector('h1');
   myHeading.textContent = 'Good to see you again, ' + storedName +  ' <p>  
   Write me again soon.' ;

...

I a (maybe two?) Stackoverflow responses about putting html into an array, but I don't think that is the same thing.

解决方案

Whilst your shell does understand what \n represents, modern browsers do not. To represent a newline on a web page, you should use the <br> element instead.

Now, as you've already experienced, you cannot interpolate an HTML element using the textContent property. This is because the textContent property represents the text of an element, not the html -- any string you set will be treated as text.

To set the inner html of an element, use the innerHTML property.

Something like this should work for your needs:

myHeading.innerHTML = 
    'Good to see you again, ' + storedName +  ' <br>  Write me again soon.' ;

这篇关于JavaScript需要发送Esc字符\\\<br/>或文本返回&lt; p&gt;到HTML /浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆