1

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)

4

1 回答 1

1

知道了,

玉自动转义 html。为了避免这种情况,可以使用!=.

所以我改为:

- 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')
于 2013-11-12T09:15:29.160 回答