简体   繁体   English

链接在ap标签中

[英]link inside a p tag in react

I have a paragraph that is enclosed in artist.bio.summary . 我有一段包含在artist.bio.summary This variable contains all the details about an artist. 此变量包含有关艺术家的所有详细信息。 This is fetched from somewhere else. 这是从其他地方获取的。 I am printing this out inside a p tag. 我正在p标签内打印出来。

My problem is there is a link inside this p tag within a a tag. 我的问题是有这里面的链接p一内标签a标签。 The a tag is like this; a标签是这样的;

 <a href="https://www.abc.xyz/music/xxx">Read more </a>

The p tag just prints this out rather than giving me a link to click. p标签只是打印出来,而不是给我单击链接。 What should I do to act accordingly? 我应该怎么做才能采取相应的行动?

I am calling this property as below: 我称这个属性如下:

<p>{artist.bio.summary}</p>


let artist = {
            bio: { summary: '' }
        };

I had set this artist.bio.summary as a string initially. 我最初已将此artist.bio.summary设置为字符串。

And an example string that i am getting is below: 我得到的示例字符串如下:

"hello <a href="https://www.abc.xyz/music/xxx">Read more </a> there"

The above string is the content of the artist.bio.summary once i received it 上面的字符串是我收到的artist.bio.summary的内容

This is a security issue and is not allowed by React (by default) so it's as expected to not evaluate the embedded html markup, but they have a workaround if you really want to. 这是一个安全问题,React不允许(默认情况下),因此可以不评估嵌入的html标记,但是如果您确实想要的话,它们可以提供解决方法。 dangerouslySetInnerHTML 危险地设置内部HTML

<p dangerouslySetInnerHTML={artist.bio.summary}></p>

But please read up on injection attacks so you understand the consequences before using this kind of dynamic evals. 但是请仔细阅读注入攻击,以便在使用这种动态评估之前了解其后果。 https://www.codeproject.com/Articles/134024/HTML-and-JavaScript-Injection https://www.codeproject.com/Articles/134024/HTML-and-JavaScript-Injection

From you description it seems that artist.bio.summary contains the entire content ie <a href="https://www.abc.xyz/music/xxx">Read more </a> . 从您的描述来看, artist.bio.summary似乎包含了整个内容,即<a href="https://www.abc.xyz/music/xxx">Read more </a> In that case what you need is dangerouslySetInnerHTML 在这种情况下,您需要dangerouslySetInnerHTMLdangerouslySetInnerHTML

<p dangerouslySetInnerHTML={{__html: artist.bio.summary}}/>

However I would suggest you to make modifications to your data such that you aren't actually passing the HTML to the React component but creating it using JSX yourself 但是我建议您对数据进行修改,以使您实际上不会将HTML传递给React组件,而是自己使用JSX创建它

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

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