如何在另一个javascript变量中使用图像url变量?

时间:2013-07-20 20:21:26

标签: javascript html

任何人都可以告诉我如何在名为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) 

2 个答案:

答案 0 :(得分:1)

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

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

变量包裹为"+variable+"

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

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

如果您在该示例中有单引号,则应该像\'一样将其转义。双引号不需要转义。

答案 1 :(得分: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>";