2013-07-17 39 views
1

我试图编写将JavaScript添加到变量中的代码。一切都会发现,直到我得到错误"SyntaxError: missing ; before statement" ...我发现,如果我删除一条线,然后它的作品(我会把// ----在该行)。请有人可以帮忙吗?“SyntaxError:missing; before statement”

$this->paint_javascript_function_text .= ' 
    function image_'.$this->archive_id.'_rollover(){ 
     document.getElementById("details").innerHTML = "'.$this->archive_name.'<br />'.$this->archive_dimensions.' - Price &#163;'.$this->archive_price.' inc Packaging"; 
    } 
    function image_'.$this->archive_id.'_fullsize(){ 
     document.getElementById("gallery").innerHTML = '; 
     $this->paint_javascript_function_text .= "'"; 

     $this->paint_javascript_function_text .= '<table width="100%" height="400"border="0" align="center"><tr><td align="right" valign="middle">'; 
     if($this->archive_id == 1) { 
      $this->paint_javascript_function_text .= '<img src="images/paint_gallery/prev_faded.png" border="0" class="paint_nav" />'; 
     } 
     else 
     { 
      $prev = $this->archive_id - 1; 
      $this->paint_javascript_function_text .= '<a href="#"><img src="images/paint_gallery/prev.png" border="0" class="paint_nav" onclick="javascript:image_'.$prev.'_fullsize();" /></a>'; 
     } 

     $this->paint_javascript_function_text .= '</td><td width="400px" height="523px" align="center" valign="middle">'.$newimage.'<br /><a href="#" class="paint_link" onclick="javascript:return_to_small_images();">Back to Gallery</a></td><td align="left" valign="middle">'; 
     if($total_num == $this->archive_id){ 
      $this->paint_javascript_function_text .= '<img src="images/paint_gallery/next_faded.png" border="0" class="paint_nav" />'; 
     } 
     else 
     { 
      $next = $this->archive_id + 1; 
      $this->paint_javascript_function_text .= '<a href="#"><img src="images/paint_gallery/next.png" border="0" onclick="javascript:image_'.$next.'_fullsize();" class="paint_nav" /></a>'; 
     } 

     $this->paint_javascript_function_text .= '</td></tr></table>'; 

     // ---- $this->paint_javascript_function_text .= '<center><span class="paintings_title">'.$this->archive_name.' - '.$this->archive_dimensions.' - '.$this->archive_price.' inc P&P</span><br /><a href="basket.php?addprint='.$this->archive_id.'"><img src="images/shop/basket_button.png" border="0" /></a></center>'; 
     $this->paint_javascript_function_text .= "'; 
      "; 
     $this->paint_javascript_function_text .= ' 
      $("#main_content").hide(); 
      $("#gallery").show(); 
      $("#paint_nav").hide(); 
    }'; 

HTML输出: “=”

function image_4_fullsize(){ 
    document.getElementById("gallery").innerHTML = '<table width="100%" height="400" border="0" align="center"><tr><td align="right" valign="middle"><a href="#"><img src="images/paint_gallery/prev.png" border="0" class="paint_nav" onclick="javascript:image_3_fullsize();" /></a></td><td width="400px" height="523px" align="center" valign="middle"><img src="images/print_gallery/Countrymans Kitchen.jpg" /><br /><a href="#" class="paint_link" onclick="javascript:return_to_small_images();">Back to Gallery</a></td><td align="left" valign="middle"><a href="#"><img src="images/paint_gallery/next.png" border="0" onclick="javascript:image_5_fullsize();" class="paint_nav" /></a></td></tr></table><center><span class="paintings_title">Countryman's Kitchen - - inc P&P</span><br /><a href="basket.php?addprint=4"><img src="images/shop/basket_button.png" border="0" /></a></center>'; 

    $("#main_content").hide(); 
    $("#gallery").show(); 
    $("#paint_nav").hide(); 
} 
+0

使用' json_encode'而不是 – bystwn22

+0

请问那林e在某处追加'''?如果您还可以将生成的HTML放在浏览器指出错误的位置,那将会很好。 – Bergi

+1

看到那行'Countryman's Kitchen' - '','$ this-> archive_name'包含一个单引号,正如我所说的,更好的方法是使用'json_encode'来设置'innerHTML'数据 – bystwn22

回答

0

您正在使用速记串联 然后连接 “正常方式”。

试试这个方法:

$this->paint_javascript_function_text = $this->paint_javascript_function_text . '<center><span class="paintings_title">'.$this->archive_name.' - '.$this->archive_dimensions.' - '.$this->archive_price.' inc P&P</span><br /><a href="basket.php?addprint='.$this->archive_id.'"><img src="images/shop/basket_button.png" border="0" /></a></center>'; 
+0

不,因为我需要追加到JavaScript .. –

1

修订
更改以下行

$this->archive_name包含一个单引号,所以你需要逃脱先用json_encode

$this->paint_javascript_function_text .= 'function image_'.$this->archive_id.'_rollover() { 
    document.getElementById("details").innerHTML = '.json_encode($this->archive_name.'<br />'.$this->archive_dimensions.' - Price &#163;'.$this->archive_price.' inc Packagin').'; 
} 
function image_'.$this->archive_id.'_fullsize() { 
    document.getElementById("gallery").innerHTML = '; 
+0

你的冷杉假设是不是这种情况http://codepad.org/Uq3w7k70 – swapnesh

+1

回答更新! – bystwn22