2013-07-20 91 views
0

任何人都可以告訴我如何正確使用myVariable和myVariable2在JavaScript變量稱爲siteContents2?此時我的代碼不顯示圖像縮略圖及其名稱。如何在另一個javascript變量裏面使用圖片url變量?

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) 

回答

1

逃避所有的雙引號,並使用此代碼使用變量:

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

只有變量被裹得像個"+variable+"

如果你有很多的雙引號,您可以使用單引號周圍的變量的內容,如:

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

如果在這個例子單引號,你應該逃避它像\'。雙引號不需要轉義。

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>"; 
相關問題