简体   繁体   English

如何在双引号内添加html标签

[英]How to add html tags inside double quotes

I have a script like below,我有一个像下面这样的脚本,

var content = "<p>Please click on the text below.<br><a "href="www.testifyme.com">More Info</a></p>"

Html,网址,

<span [innerHtml]="content"></span>

Here the problem is with my anchor tag,how to add href value in double quotes since the "<a href:"www.....>这里的问题是我的锚标记,如何在双引号中添加 href 值,因为"<a href:"www.....>

Here I am getting error at href.Can anyone please help me.Thanks.在这里,我在 href 处遇到错误。任何人都可以帮助我。谢谢。

您可以像这样转义"\\"或者您可以双引号使用单引号' a hrefclasses

When i have just entered the job, I usually put them in single quotes or concat strings当我刚入职时,我通常将它们放在单引号或连接字符串中

var content = '<p>Please click on the text below.<br><a href="www.testifyme.com">More Info</a></p>'

I have known to use slash, later我知道使用斜杠,后来

var content =  "<p>Please click on the text below.<br><a href=\"www.testifyme.com\">More Info</a></p>"

When i have known ES6, i use ```当我知道 ES6 时,我使用 ```

var content = `<p>Please click on the text below.<br><a href="www.testifyme.com">More Info</a></p>`

The problem is in your javascript variable.问题出在您的 javascript 变量中。

You have two bugs.你有两个错误。 The first one is the double quotes before href those are not needed.第一个是不需要的href之前的双引号。

The other problem is the quotes for the actual link, that is causing a syntax error, since javascript thinks you are closing the string variable.另一个问题是实际链接的引号,这会导致语法错误,因为 javascript 认为您正在关闭字符串变量。

You have several options to fix that.您有多种选择来解决这个问题。

Use single quotes for the string variable:对字符串变量使用单引号:

var content = '<p>Please click on the text below.<br><a href="www.testifyme.com">More Info</a></p>';

Note the first and last quotes are single quotes.注意第一个和最后一个引号是单引号。

Or to escape the double quotes using the escape character \\或者使用转义字符\\转义双引号

var content = "<p>Please click on the text below.<br><a href=\"www.testifyme.com\">More Info</a></p>";

A third option (I would not personally recommended it for this specific case, but I included it for reference) could be to use template literals with the ` character:第三个选项(我个人不会针对这种特定情况推荐它,但我将其包含在内以供参考)可能是使用带有`字符的模板文字:

var content = `<p>Please click on the text below.<br><a href="www.testifyme.com">More Info</a></p>`;

Not the first and last are backticks `不是第一个和最后一个是反引号`

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM