问题
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
<
,follow on Twitter:
<a href="http://twitter.com/user" target="_blank">twitter.com/user</a>
>
<!--,follow on Twitter: <a href="http://twitter.com/user" target="_blank"-->
twitter.com/user
>
<
,follow on GitHub:
<a href="http://github.com/user" target="_blank">github.com/user</a>
>
<!--,follow on GitHub: <a href="http://github.com/user" target="_blank"-->
github.com/user
>
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