Javascript js中的HTML连接失败 html+='
它不起作用,因为引号不正确。您有嵌套的双引号,这导致了问题 您的字符串在生成时如下所示Javascript js中的HTML连接失败 html+=',javascript,html,Javascript,Html,它不起作用,因为引号不正确。您有嵌套的双引号,这导致了问题 您的字符串在生成时如下所示 html += '<div style="background: url("http://graph.facebook.com/' + photo + '/picture?type=normal") background-size: cover;>'; 浏览器将看到 <div style="background: url("http://graph.facebook.co
html += '<div style="background: url("http://graph.facebook.com/' + photo
+ '/picture?type=normal") background-size: cover;>';
浏览器将看到
<div style="background: url("http://graph.facebook.com/xxx/picture?type=normal") background-size: cover;">
试着把它分解一下。连接时,您希望它返回什么?用一个值替换此。封面照片。例如,假设这张封面照片是“foo” 这将有助于:
html += '<div style=\'background: url("http://graph.facebook.com/' + this.cover_photo
+ '/picture?type=normal") background-size: cover;\'>';
<div style="background: url("http://graph.facebook.com/' + 'foo' + '/picture?type=normal") background-size: cover;>
您的引号已关闭,如上面StackOverflow的语法突出显示所示。您需要对内部引号进行转义,或者对url
<div style="background: url("http://graph.facebook.com/foo/picture?type=normal") background-size: cover;>
谢谢:)那样试试看。工作正常。 用以下代码替换您的代码:
html = templete_render(html, this.cover_photo);
html+='';
在开始附加HTML变量之前,您是否声明了该变量?在你的代码示例上面试试这个var html=''代码>。请在此行上方显示更多代码更新变量不会更新页面上实际显示的内容,您必须使用someElement.innerHTML=html代码>@alexpls是的,这是导致问题的唯一一行。缺少双引号或单引号可以发布更好的答案吗?在哪里?当然可以。你的答案的更新版本很好。(我现在已经忘记了你最初的措辞是什么,但当时我认为它没有充分回答一个抱怨“连接”的问题,因为问题实际上与连接无关。我将删除该评论。)和@UselessCode-绝对不正确。单引号即使在XHTML中也有效。@epascarello-Hmmm。。。看来你是。我可以发誓,在过去,当我不小心使用了一个引用时,W3C验证程序对我很生气。这是不对的:style
属性中有两个CSS属性,但是你在第一个属性之后结束了属性。你不能在background:p之前添加这两个属性吗?比如:-?嗯?只需将关闭的”
移动到背景大小之后即可。
function templete_render(html, cover_photo){
return html + "<div style='background : url(http://graph.facebook.com/" + cover_photo + "/picture?type=normal) background-size: cover;'>";
} // end of templete_render
html = templete_render(html, this.cover_photo);
html += '<div style="background: url(http://graph.facebook.com/' +photo+ '/picture?type=normal); background-size: cover;">';