render javascript function return with links inside

。_饼干妹妹 提交于 2019-12-11 13:49:44

问题


I am using this code in a .jade file, inside a for loop to iterate and render html with a object's values:

- var linkExist = function(i){
-   if (result[i]){
-       var html = ',follow on ' + i + ': <a href="' + result[i] + '" target="_blank">' + result[i].split("http://")[1] + '</a>';
-       return html;
-   };
- }


#{linkExist('Twitter')}
#{linkExist('GitHub')}

It adds extra comments and renders a extra < before na d a > after, like

&lt;
,follow on Twitter: 
<a href="http://twitter.com/user" target="_blank">twitter.com/user</a>
&gt;
<!--,follow on Twitter: <a href="http://twitter.com/user" target="_blank"-->
twitter.com/user
&gt;
&lt;
,follow on GitHub: 
<a href="http://github.com/user" target="_blank">github.com/user</a>
&gt;
<!--,follow on GitHub: <a href="http://github.com/user" target="_blank"-->
github.com/user
&gt;

btw, if I use

=linkExist('Twitter')
or
| #{linkExist('Twitter')}

It renders html as text, but the correct content. (but as text, not html)


回答1:


Got it,

jade escapes the html automatically. To avoid that one can use !=.

So I changed to:

- var linkExist = function(i){
-   if (result[i]){
-       var html = ' ,follow on ' + i + ': <a href="' + result[i] + '" target="_blank">' + result[i].split("http://")[1] + '</a>';
-       return html;
-   };
- }


!= linkExist('Twitter')
!= linkExist('GitHub')


来源:https://stackoverflow.com/questions/19924139/render-javascript-function-return-with-links-inside

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!