2014-10-28 78 views
2

我問這個問題,這是我得到的幫助解決了相當快: Reverse the way of function output倒車功能打破了代碼

然而,當我做什麼建議,它打破了JavaScript的工作方式。

如果我這樣做

$output = '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">' . $output; 

取而代之的是:

$output .= '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">' 

的JavaScript停止工作。但是,產出倒過來了,因爲它應該是。這怎麼能解決,但仍然有輸出反轉?

我在這裏粘貼了全部函數:http://paste.ofcode.org/kerFDpAUmjS6tk9jqbpzyi(113行代碼)。循環開始於第39行,並開始輸出管線77

最好的問候,帕特里克

+1

只是一個小的評論的事情;爲什麼不使用[heredocs](http://stackoverflow.com/a/5673478/899126)來保存多行字符串,尤其是因爲您仍然可以在其中嵌入變量? – 2014-10-28 18:58:33

+0

更具相關性:你認爲它打破了JavaScript的工作方式是什麼意思?控制檯日誌中是否顯示任何內容?如果您查看頁面源代碼,那麼生成的HTML看起來是否正確? – 2014-10-28 19:01:46

+0

@ChrisForrence呃,heredocs我以前沒有用過 - 我會研究這個,謝謝! - 無論輸出如何生成,頁面源都完全相同。這是爲什麼我不明白這是行不通的原因之一。 – Patrick 2014-10-28 19:21:18

回答

0

在你的榜樣這個

$output = '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">' . $output; 

將創建此$output

<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'"><div class="w-portfolio"> 
         <div class="w-portfolio-h"> 
          <div class="w-portfolio-list"> 
           <div class="w-portfolio-list-h"> 

提示:在代碼中使用var_dump($output)echo $ouput,以便您可以查看存儲的內容在$輸出變量。就像你現在的代碼似乎你會有一個混亂的HTML結構。

編輯:

要初始化的第35行,現在我正在寫一些僞代碼應該如何工作循環的$output變量外:

//line 34 
    $output = '<div class="w-portfolio"> 
        <div class="w-portfolio-h"> 
         <div class="w-portfolio-list"> 
          <div class="w-portfolio-list-h">'; 

    foreach($posts as $post) { // Note: This is the while loop in your example 

     $output .= '<a href="javascript:void(0);" data-id="'.$post->ID.'">'; //notice the .= which appends the string to variable declared on line 35 

    } 
    $output .=    '</div> 
         </div> 
        </div> 
       </div>'; 

    return $output; 

不要使用$output .= $output$output = 'bla bla' . $ouput這裏的任何地方。這會破壞你想用你的代碼創建的HTML結構。注意您的$輸出變量以$output = '<div class="w-portfolio">開頭。


刪除. $output;</div>'. $output;在你的代碼行105。

試試這個PasteOfCode