[英]how to use json_encode() on a php array, and also how to use jQuery.getJSON()?
http://api.jquery.com/jQuery.getJSON/
http://www.w3schools.com/jquery/ajax_getjson.asp
http://api.jquery.com/jQuery.parseJSON/
http://php.net/manual/zh/function.json-encode.php
http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/
这些是我一直在研究的网站,试图将我的头放在json上
这是我的PHP
$fvsc=array();
query_posts('meta_key=featuredv');
if (have_posts()) :
while (have_posts()) :
the_post();
$fvsc []= "\n" . '<div id="featuredv-' . "$wp_query->current_post\">" . "$post->post_content;" . '</div>';
endwhile;
endif;
$jsonObject= json_encode($fvsc);
这是我的.js
jQuery(document).ready(function($){
jQuery.getJSON("<?php TEMPLATEPATH . '/featuredv.php'?>",function(data){
var fvsJA=jQuery.parseJSON(<?php $jsonObject; ?>);
var fts = $("#ttContent>[id^=featuredt]");
var fvs = $("#inCenter>[id^=featuredv]");
});
fts.click( function(){
$("#inCenter>[id^=featuredv]").replaceWith( function(){
$(fvsJA).eq(fts.index(this))
});
});
我想做的是将$ fvsc数组变成jQuery数组。 $ fvsc是一个视频数组,单击fts时,应将div的内容替换为具有与单击fts相同的索引的另一个视频。 如果仅将所有fts和fvs加载到一个位置,则可以在此处使用click功能,效果很好,但是如果您回显5个视频,然后回显10个视频,则该站点的加载情况越来越糟。 我以为如果可以将php数组转换为可加载json字符串的jquery对象,则在单击时将其转换回对象并替换div内容,负载会更好。
我在您的代码中看到了几个问题:
$fvsc []= "\\n" . '<div id="featuredv-' . "$wp_query->current_post\\">" . "$post->post_content;" . '</div>';
我认为您不能将类的变量用双引号引起来。 应该是这样的:
$fvsc []= '<div id="featuredv-' . $wp_query->current_post . '">' . $post->post_content . '</div>';
JS中存在问题,特别是访问URL的方式。 您会发现,wordpress中的ajax与常规php有所不同。 在此处查找更多信息: https : //wordpress.stackexchange.com/questions/9231/whats-the-preferred-method-of-writing-ajax-enabled-plugins/9236#9236
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.