【问题标题】:Javascript & JQuery Troubles - Grabbing & Writing YouTube Data to PageJavascript 和 JQuery 问题 - 抓取 YouTube 数据并将其写入页面
【发布时间】:2023-04-02 22:46:01
【问题描述】:

我会说我对 Javascript 和 JQuery 有一点经验,但是在我花在我的一个新网站上的时间里,什么都没有。基本上,我使用 PHP 将视频 ID 写入 javascript 代码,然后执行该代码以获取 YouTube 视频的标题、上传者、观看次数和长度。在我的另一个网站(Mechabyte.com 上的“最新视频”侧边栏)上一切正常,但在我的新网站 FirstPersonTheater.net 上,没有任何效果。我已经包含了 JQuery 库,但是我的脚本没有将信息写入每个帖子的 div 字段。我已经合理化这可能只是使用准系统主题的效果(我正在构建自己的主题,但希望在构建它时解决问题),但是因为我有所有要求(标题中的 JQuery 库+ 脚本)我认为它应该仍然有效。这是我的源代码,你也可以访问the site 并检查一些元素,看看我是否遗漏了什么。

基本布局(假设视频ID为0000000000000:

<div class="index_post">
<a href="<?php the_permalink() ?>">
   <div class="image">
      <div class="container" style="background:url('http://img.youtube.com/vi/0000000000000/0.jpg');">
                                                                                                </div>
             <div id="data_time<?php the_ID(); ?>" style="position:absolute;z-index:9;text-decoration:none;bottom:2px;right:2px;font-size:10px;color:#fff;background:#000;padding:0px 2px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;opacity:0.75;">
                    3RR:0R!
             </div>
      </div>
      <div class="postinfo">
             <div class="data_uploadedby<?php the_ID() ?>"></div>
             <div class="data_views<?php the_ID() ?>"></div>                
      </div>
      <div class="extras">
      <script type="text/javascript">
            function ytindex<?php the_ID(); echo time() ?>( data )
                    {
                    $('#data_time <?php the_ID() ?>').html( secondsToHms( data.entry[ "media$group" ][ "yt$duration" ].seconds ) );
                    $('.data_postedby<?php the_ID() ?>').html( 'by ' + data.entry[ "author" ][ 0 ].name.$t );
                    $('.data_uploadedby<?php the_ID() ?>').html( data.entry[ "title" ].$t );
                    $('.data_views<?php the_ID() ?>').html( data.entry[ "yt$statistics" ].viewCount + ' views</h3>' );
                    }
      </script>
      <script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/0000000000000?v=2&callback=ytindex<?php the_ID(); echo time() ?>"></script>
   </div>
</a>
</div>

结果如下(缩略图等 CSS 元素显示正常,因为它们是使用 PHP 处理的,其他元素均未加载):http://i.stack.imgur.com/0HYKQ.png(SO rep 的链接 b/c)

提前致谢!根据经验,我可以说这个网站上的大多数人都是很棒的帮手! -马特

【问题讨论】:

    标签: javascript jquery jquery-ui wordpress


    【解决方案1】:

    jquery 库已经包含在当前版本的 wordpress 中。使用wp_enqueue_script 添加jquery 库。 不要只是将脚本添加到 header.php 文件中...

    那么你还需要使用这个准备好的函数来兼容wordpress。

    jQuery(document).ready(function($){
    // now you can use jQuery code here with normal $ shortcut formatting
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-02
      • 2021-03-10
      • 2018-05-22
      • 2017-02-10
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      相关资源
      最近更新 更多