2009-12-29 43 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