0

谁能告诉我如何在名为 siteContents2 的 javascript 变量中正确使用 myVariable 和 myVariable2?此时我的代码不显示图像缩略图及其名称。

 var siteContents2=""
            +"<li>"
            +"<img src=""+myVariable2+"" width=\"180\" height=\"148\""
            +"alt=\"'+myVariable+'\" class=\"png\">"
            +"<a href=\"'+myVariable+'"  class=\"corners\">&nbsp;<\/a>"
            +""
            +"    "
            +"        "
            +"            "
            +"<div class=\"thumbnail_label\">mango<\/div>"
            +"            "
            +"        "
            +"    "
            +""
            +"<div class=\"details\">"
            +"<div class=\"title\">"
            +"      <a  href="
            +"      \"'+myVariable+'">"+myVariable+"<\/a>"
            +"      <span class=\"season\">2<\/span>"
            +"    <\/div>"
            +"    <ul class=\"subject\">"
            +"      <li>mango sesaon<\/li>"
            +"    <\/ul>"
            +"    <ul class=\"sub-info\">"
            +"      <li class=\"location\">us<\/li>"
            +"      <li class=\"price\">2 dollar<\/li>"
            +"    <\/ul>"
            +"  <\/div>"
            +"<\/li>";

         $('#myDiv').append(siteContents2) 
4

2 回答 2

1

转义所有双引号并使用此代码来使用变量:

var str = "Text text "+variable_name+" continuing text";

只有变量像"+variable+".

如果您有很多双引号,您可以使用单引号来围绕您的变量内容,如下所示:

var str = '<div class="test">'+variable_name+'</div>';

如果该示例中有单引号,则应将其转义为\'. 双引号就不需要转义了。

于 2013-07-20T20:26:18.213 回答
1
var siteContents2=""
            +"<li>"
            +"<img src='"  + myVariable2 + "' width=\"180\" height=\"148\""
            +"alt='" + myVariable + "' class=\"png\">"
            +"<a href='" +myVariable+ "'  class=\"corners\">&nbsp;<\/a>"
            +""
            +"    "
            +"        "
            +"            "
            +"<div class=\"thumbnail_label\">mango<\/div>"
            +"            "
            +"        "
            +"    "
            +""
            +"<div class=\"details\">"
            +"<div class=\"title\">"
            +"      <a  href="
            +"      '" + myVariable + "'>"+myVariable+"<\/a>"
            +"      <span class=\"season\">2<\/span>"
            +"    <\/div>"
            +"    <ul class=\"subject\">"
            +"      <li>mango sesaon<\/li>"
            +"    <\/ul>"
            +"    <ul class=\"sub-info\">"
            +"      <li class=\"location\">us<\/li>"
            +"      <li class=\"price\">2 dollar<\/li>"
            +"    <\/ul>"
            +"  <\/div>"
            +"<\/li>";
于 2013-07-20T20:49:43.403 回答