我正在尝试在页面中插入标签。但在一个标签内,它有单引号和双引号,所以我尝试将该部分放入变量Var内容。有人能告诉我为什么代码运行后onclick上没有显示这个var内容吗?
var content = "javascript:window.open('https://ds.contactatonce.com/ds/p10819departmentselector.htm?OriginationUrl='+encodeURIComponent(location.href),'','resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=no,status=no,height=520,width=450');return false;"
$('<a style ="float: left; margin-top: 20px" href="#" onclick=" + content + " ><img onerror="this.height=0;this.width=0;" border="0" alt="Click to instant message Representative Now!" title="" src="https://dm5.contactatonce.com/getagentstatusimage.aspx?MerchantId=270075&ProviderId=10819&PlacementId=10"></a>').insertBefore('.header-contact-phone');
之前就是这样,但它一直显示错误
$('<a style ="float: left; margin-top: 20px" href="#" onclick="javascript:window.open('https://ds.contactatonce.com/ds/p10819departmentselector.htm?OriginationUrl='+encodeURIComponent(location.href),'','resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=no,status=no,height=520,width=450');return false;" ><img onerror="this.height=0;this.width=0;" border="0" alt="Click to instant message !" title="Click to instant message a Now!" src="https://dm5.contactatonce.com/getagentstatusimage.aspx?MerchantId=270075&ProviderId=10819&PlacementId=10"></a>').insertBefore('.header-contact-phone');
答案 0 :(得分:1)
在引号前使用转义字符\ 像这样:
var y = "We are the so-called \"Vikings\" from the north.";
请看这个链接:
https://www.w3schools.com/js/js_strings.asp
https://www.w3schools.com/js/tryit.asp?filename=tryjs_strings_escape