我正在尝试以动态方式创建以下HTML
<p id="address">Name
<br> City,
<br> address,
<br> +9023562567, +91 7023344335
<br>
<strong>Potential (MT) :</strong> 4000
</p>
我是这样尝试的
var html= '';
var name = 'Mark' ;
var city = 'California'
var address = 'Losangles';
var phone1 = '9023562567' ;
var phone2 = '7023344335' ;
var mt= 'Potential (MT)' ;
var mtvalue = '4000' ;
html += '+name+'<br>\n\
'+city+',<br>\n\
'+address+',<br>\n\
+'+phone1+', +'+phone2+'<br>\n\
<strong>'+mt+' :</strong> +mtvalue+</p>\n\
$("#id").html(html);
但是我得到了Uncaught SyntaxError:意外的令牌ILLEGAL
你可以告诉我如何解决这个问题请原谅这是一个愚蠢的问题。
答案 0 :(得分:3)
您无法使用\
html += name + '<br>' + city + ',<br>' + address + ',<br>+' + phone1 + ', +' + phone2 + '<br><strong>' + mt + ' :</strong> +mtvalue+</p>';
修改强>
您使用错误的选择器添加html
:
$('#address').html(html);
// ^^^^^^^
答案 1 :(得分:1)
<强>第一)强>
您的 ID不正确使用#address
$("#address").html(html);
<强>第二)强> 你的串联html应该是:
html += name+'<br>\n\'+
city+',<br>\n\'
address+',<br>\n\+'
+phone1+', +'
+phone2+'<br>\n\<strong>'
+mt+' :</strong> +mtvalue+</p>\n\';
答案 2 :(得分:0)
您的html
字符串错误。试试这个:
var html= '';
var name = 'Mark' ;
var city = 'California'
var address = 'Losangles';
var phone1 = '9023562567' ;
var phone2 = '7023344335' ;
var mt= 'Potential (MT)' ;
var mtvalue = '4000' ;
html += name+'<br>'+city+',<br>'+address+',<br>'+phone1+', '+phone2+'<br><strong>'+mt+' :</strong> '+mtvalue+'</p>';
$("#address").html(html);
答案 3 :(得分:0)
你在这里有错误:
<strong>'+mt+' :</strong> +mtvalue+</p>\n\
将其更改为:
<strong>'+mt+' :</strong> '+mtvalue+'</p>';
你也应该改变
html += '+name+'<br>\n\
与
html += '<p>'+name+'<br>
并删除字符串中的所有\ n \
答案 4 :(得分:0)
试试这个,\n
<br>
不需要var html= '';
var name = 'Mark' ;
var city = 'California'
var address = 'Losangles';
var phone1 = '9023562567' ;
var phone2 = '7023344335' ;
var mt= 'Potential (MT)' ;
var mtvalue = '4000' ;
html += name+'<br>'+city+',<br>'+address+',<br>+'+phone1+', +'+phone2+'<br><strong>'+mt+' :</strong>' +mtvalue+'</p>';
$("#address").html(html);
。
ActionBar
答案 5 :(得分:0)
这应该有效,经过测试
var name = 'Mark' ;
var city = 'California'
var address = 'Losangles';
var phone1 = '9023562567' ;
var phone2 = '7023344335' ;
var mt= 'Potential (MT)' ;
var mtvalue = '4000' ;
var html = name+"<br>"+
city+",<br>"+
address+",<br>"+
phone1+", "+phone2+"<br>"+
"<strong>"+mt+" :</strong> "+mtvalue+"</p>";
$("#id").html(html);
答案 6 :(得分:0)
var html= '';
var name = 'Mark' ;
var city = 'California'
var address = 'Losangles';
var phone1 = '9023562567' ;
var phone2 = '7023344335' ;
var mt= 'Potential (MT)' ;
var mtvalue = '4000' ;
html += name+'<br>\n\
'+city+',<br>\n\
'+address+',<br>\n\
+'+phone1+', +'+phone2+'<br>\n\
<strong>'+mt+' :</strong>' +mtvalue+'</p>\n\
';
$("#address").html(html);