2009-12-29 112 views
0

我正在使用jQuery在Drupal中工作。如何在標籤中插入一個php $變量。

$(document).ready(function(){ 
    $("#comment-delete-<?php print $variable ?>").click(function(){ 
     $("div.comment-<?php print $variable ?> span.body").replaceWith("new text"); 
    }); 
}) 

$(document).ready(function(){ 
    $("#comment-delete-". $variable).click(function(){ 
     $("div.comment-". $variable ." span.body").replaceWith("new text"); 
    }); 
}) 

有幾件事情來clearify。 我在Drupal運行,因此完整的代碼如下所示:

<?php 
drupal_add_js (
    '$(document).ready(function(){ 
     $("#comment-delete-"' print $comment->cid; ').click(function(){ 
      $("div.comment-"' print $comment->cid; '" span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>"); 
     }); 
    })', 
'inline'); 
?> 

,但它仍然沒有工作。

更新:我嘗試以下,但使用的變量時......奇怪的是仍然沒有工作

<?php 
$testing = "42"; 
drupal_add_js (
    '$(document).ready(function(){ 
     $("#comment-delete-"'. $testing .').click(function(){ 
      $("div.comment-"'. $testing .'" span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>"); 
     }); 
    })', 
'inline'); 
?> 

如果我用數字「42」,而不是變量,它的工作原理,但不是。

+0

你甚至嘗試過嗎?第一種方法應該爲你工作 – 2009-12-29 06:04:40

+0

我在Drupal中,我正在使用<?php drupal_add_js(' $(document).ready(function(){ $(「#comment-delete - ?> print $ variable < (「new text」); }); }) (「div.comment - ?> print $ variable <?php span.body」)。 ','一致'); ?> 但它不工作 – Tim 2009-12-29 06:12:10

+0

沒關係...這是行不通的...... 但你可以看到,我在PHP已經<?PHP drupal_add_js(***)?> – Tim 2009-12-29 06:13:46

回答

3

基於您的評論:

<?php 
    drupal_add_js (' 
     $(document).ready (function() { 
      $("#comment-delete-' . $variable . '").click (function() { 
       $("div.comment-' . $variable . ' span.body").replaceWith ("new text"); 
      }); 
     }) 
    ','inline'); 
?> 

你應該串聯的$variable,而不是print -ing它

+0

這樣做的技巧 – Tim 2009-12-29 06:37:56

6
$(document).ready(function(){ 
    $("#comment-delete-<?php print $variable ?>").click(function(){ 
     $("div.comment-<?php print $variable ?> span.body").replaceWith("new text"); 
    }); 
}) 

因爲PHP在加載頁面之前執行,所以第二種方法將不起作用。實際上,第二種語言混合了在不同時間運行的兩種不同的語言,這意味着......它仍然不起作用。


這是發生了什麼事。

瀏覽器請求頁面

PHP創建的HTML頁面
PHP搜索<?php ?>標籤的文件,並運行它們內部的代碼:

$(document).ready(function(){ 
    $("#comment-delete-<?php print $variable ?>").click(function(){ 
     $("div.comment-<?php print $variable ?> span.body").replaceWith("new text"); 
    }); 
}) 

上面的例子會在解析後創建這個:

$(document).ready(function(){ 
    $("#comment-delete-mytag").click(function(){ 
     $("div.comment-mytag span.body").replaceWith("new text"); 
    }); 
}) 

服務器發送網頁瀏覽器

瀏覽器讀取網頁

JavaScript執行:

$(document).ready(function(){ 
    $("#comment-delete-mytag").click(function(){ 
     $("div.comment-mytag span.body").replaceWith("new text"); 
    }); 
}) 

如果你注意到,PHP簡單地創建了網頁是發送到瀏覽器。所以你使用PHP來做的就是創建Javascript代碼。在PHP中工作時,您實際上不必遵循任何Javascript語法規則。當它到達瀏覽器時,您必須簡單地使Javascript語法正確。 AKA你可以插入你想要的所有<?php ?>標籤,只要當頁面到達瀏覽器時,它就是有效的Javascript。


如果您傳遞的代碼放到一個函數,就像您的評論說,你要創建一個字符串,它被封裝在引號,在這種情況下,你的第二個例子是正確的:

drupal_add_js (
    '$(document).ready(function(){ 
     $("#comment-delete-'. $variable . ').click(function(){ 
      $("div.comment-'. $variable . ' span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>"); 
     }); 
    })', 
'inline'); 
0

Ooops,沒關係,我只是意識到確切的答案已經發布,我只是沒有足夠小心去跟着它..

+0

請點擊我們的答案旁邊的複選框以標記爲正確。日Thnx!很高興你解決了你的問題。 – 2009-12-29 06:37:36