简体   繁体   English

在jQuery追加中包括PHP

[英]including php within a jquery append

I have read append php in jquery and it appears that JSON may be the solution but I'm unsure how to combine the 2 blocks of code. 我已经阅读了jquery中的append php,看来JSON可能是解决方案,但是我不确定如何组合这两个代码块。 Any assistance at all appreciated. 任何帮助都表示赞赏。

Basically if I have the following JQuery: 基本上,如果我有以下JQuery:

$( "li.share-google-plus-1 a span" ).append( '<span class="share-count">x</span>' );

This is tested at http://jsfiddle.net/HSCzf/1/ 已在http://jsfiddle.net/HSCzf/1/上进行了测试

However in place of 'x' I need to include PHP to fetch the content... something like: 但是,代替“ x”,我需要包括PHP来获取内容……类似:

<?php 
$obj=new shareCount(get_permalink( $post->ID ));  
echo $obj->get_plusones();
?>

Can anyone assist me insert this PHP where the 'x' is? 谁能协助我将此PHP插入“ x”所在的位置?

You can do this in several ways: 您可以通过几种方式执行此操作:

If you have the jquery and php together in 1 file: 如果您将jquery和php放在一个文件中:

<?php
$obj=new shareCount(get_permalink( $post->ID ));  
?>

$( "li.share-google-plus-1 a span" ).append( '<span class="share-count"><?php echo $obj->get_plusones(); ?></span>' );

If your jquery is in a seperate js file, you can choose to add the url in the <head> : 如果您的jQuery在单独的js文件中,则可以选择在<head>添加URL:

<head>
  <script>
    var plusone_link = '<?php echo $obj->get_plusones();  ?>';
  </script>
</head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM