我有这个url,我必须作为javascript字符串传递。我知道我需要逃避角色,但无论我做什么,我都无法做到这一点?
var v = "<a href="www.youbigboy.com/showthread.php?t=1847">www.youbigboy.com/showthread.php?t=1847</a>";
答案 0 :(得分:2)
您在使用双引号声明的字符串中使用双引号。你可以:
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
\
var v = "<a href=\"www.youbigboy.com/showthread.php?t=1847\">www.youbigboy.com/showthread.php?t=1847</a>";
var v = '<a href="www.youbigboy.com/showthread.php?t=1847">www.youbigboy.com/showthread.php?t=1847</a>';
答案 1 :(得分:1)
您可以使用单引号'
来区分内部引号,也可以转义内部引号\"
代码变为:
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
或
var v = "<a href=\"www.youbigboy.com/showthread.php?t=1847\">www.youbigboy.com/showthread.php?t=1847</a>";
答案 2 :(得分:0)
也许这个
var url = "www.youbigboy.com/showthread.php?t=1847"; //or other dynamic url's
var sb = "<a href='" + url + "'>big boy url</a>";
来源:is the + operator less performant than StringBuffer.append()