作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在替换 api 的答案并正确格式化时遇到问题
answerFromAPI = "textword textword textword textword textword.\n\nFeel free to ask me questions from this site:\nhttps://google.com \n"
我想按如下方式格式化和呈现它:
textword textword textword textword textword.
Feel free to ask me questions from this site:
https://google.com /*this should be clickable link*/
为此,我创建了两个函数。第一个用于文本链接
let textLink = answerFromAPI.replace(/(((https?\:\/\/)|(www\.))(\S+))/gi,
function (x) {
console.log(x)
return '<a href="' + x + '" target="_blank">' + x + '</a>';
})
第二个换行
let newLine = answerFromAPI.replace(/(\n?\\n)/ig, function(n) {
console.log(n)
return '<br />'
})
我试图在将这些应用到函数之后将其插入数组让我们以测试链接为例
array= []
array.push(textLink)
之后我在 jsx 中渲染数组
render() {
let temp = []
this.array.map((a,index)=>{
temp.push(<p>{a}</p>)}
)}
并返回如下
return <div>
{temp}
</div>
如何在前端正确呈现我的消息。我尝试了多种方式,但我遇到了一个问题,比如函数的响应是作为字符串而不是 HTML 标记传递的。
我该怎么做?
最佳答案
您需要在元素上设置 innerHTML
属性以使其呈现 HTML 而不是字符串。
return <div innerHTML={temp} />;
参见 https://stenciljs.com/docs/templating-jsx#complex-template-content了解更多详情。
关于javascript - 使用模板将 https 链接替换为 <a>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539770/
在 Vaadin 7.0,显示时JavaBean Table 中的数据与 BeanContainer ,用新数据刷新表的正确方法是什么? 最佳答案 该表通过监听器监视表项的属性。如果您通过表的 Ite
首先,我使用的是带有 Axis2 1.6.2 的 eclipse,我正在 tomcat 6 上部署我创建的 Web 服务。Web 服务是在 eclipse 中通过自上而下的方法创建的。 我被要求使对我
我已将 Rails 3.1.1 应用程序升级到 Rails 3.1.3,现在,对于每个请求,它仅响应错误数量的参数(3 for 1)。不幸的是,它没有说明错误在哪里,并且应用程序跟踪为空。我认为存在一
我是一名优秀的程序员,十分优秀!