开发者

String inside onclick

开发者 https://www.devze.com 2023-03-13 12:04 出处:网络
So heres my code html += \"<li class=\'status-\"+peeps[i].online_presence+\"\' onclick=\'show_connect_message(\"+peeps[i].uid+\");streamPublish(\"+peeps[i].uid+\",\"+name+\");\'><a href=\'#\

So heres my code

html += "<li class='status-"+peeps[i].online_presence+"' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","+name+");'><a href='#' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","+name+");return false;'>"+peeps[i].name+"</a></li>";

Basically, when inspecting the element I can see it is outputting a UID and name. Here is the line:

<li class="status-active" onclick="show_connect_message(503141088);streamPublish(503141088,David);"><a href="#" 开发者_如何学JAVAonclick="show_connect_message(503141088);streamPublish(503141088,David);return false;">David</a></li>

So what am I doing wrong. I need the name (David) in double quotes or single quotes because otherwise the console says that it can't find the function referenced.

Warning: Javascript Noob

Thanks for your help!


change

streamPublish("+peeps[i].uid+","+name+")

to

streamPublish("+peeps[i].uid+",\""+name+"\")

the backslash will escape the double quotes and will not break the string


Try this

html += "<li class='status-"+peeps[i].online_presence+"' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","""+name+""");'><a href='#' onclick='show_connect_message("+peeps[i].uid+");streamPublish("+peeps[i].uid+","""+name+""");return false;'>"+peeps[i].name+"</a></li>";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号