请教大佬如何在首页的PHP循环中调用同一个js

模板开发以及发布测试

版主: seita

回复
zbxm
帖子: 10
注册时间: 2020年 4月 13日 17:41

请教大佬如何在首页的PHP循环中调用同一个js

帖子 zbxm »

各位大佬,你们好。可以麻烦你们在百忙之中帮我修改修改一下下面的代码吗,让它每条都可以使用最下面的倒计时js,顺便再加一个判断,如果<?php $this->fields->expire(); ?>有内容,js启动,如果没有的话,就不需要显示js,真的非常麻烦你们了,非常感谢。我测试的页面为https://www.zhengbanxianmian.com

图片
图片
图片

代码: 全选

<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
 $this->need('header.php');
?>

<div class="layui-container">
    <div class="layui-row layui-col-space15 main">
        <div class="layui-col-md9 layui-col-lg9">
            <?php while($this->next()): ?>
                <div class="title-article list-card">
                    <div class="list-pic"><a href="<?php $this->permalink() ?>" title="<?php $this->title() ?>"><img src="<?php echo thumb($this); ?>" alt="<?php $this->title() ?>" class="img-full"></a></div>
                    <a href="<?php $this->permalink() ?>">
                        <h1><?php $this->title() ?></h1>
                        <p>
                            <?php $this->excerpt(300, '...'); ?>
                        </p>
                    </a>
                    <div class="title-msg">
                        <span><i class="layui-icon"></i> <?php $this->category(','); ?></span>
                        <span><i class="layui-icon"></i> <?php $this->date('Y-m-d A'); ?> </span>
                        <span class="layui-hide-xs"><i class="layui-icon"></i> <?php get_post_view($this); ?>℃</span>
                        <span class="layui-hide-xs"><i class="layui-icon"></i> <?php $this->commentsNum('%d'); ?>条</span>
                        <span><i class="layui-icon"></i> <a id="test"</a></span>
                    </div>
                </div>
            <?php endwhile; ?>
            <div class="page-navigator">
                <?php $this->pageNav('«', '»', 1, '...', array('wrapTag' => 'div', 'wrapClass' => 'layui-laypage layui-laypage-molv', 'itemTag' => '', 'currentClass' => 'current', )); ?>
            </div>
        </div>
       
        <?php $this->need('sidebar.php'); ?>

    </div>
</div>

<script>
layui.use('util', function(){
  var util = layui.util;
  var endTime = new Date("<?php $this->fields->expire(); ?>").getTime() //假设为结束日期,注意js的月份是0-11,!
  ,serverTime = new Date().getTime(); //假设为当前服务器时间,这里采用的是本地时间,实际使用一般是取服务端的
  util.countdown(endTime, serverTime, function(date, serverTime, timer){
    var str = date[0] + '天' + date[1] + '时' +  date[2] + '分' + date[3] + '秒';
    layui.$('#test').html(str);
  });
});
</script>

<?php $this->need('footer.php'); ?>
头像
傻子哦
帖子: 10
注册时间: 2018年 7月 17日 17:23
联系:

Re: 请教大佬如何在首页的PHP循环中调用同一个js

帖子 傻子哦 »

直接用php判断字段在不在再输出js代码。
另外一个页面只放一个ID,你的ID要用test,应该命名为 "test+随机数(一个独立的数字母什么一连串有特征的就行)"。
蠢办法就是while里每输出一个文章就输出一段js。
zbxm
帖子: 10
注册时间: 2020年 4月 13日 17:41

Re: 请教大佬如何在首页的PHP循环中调用同一个js

帖子 zbxm »

傻子哦 写了:直接用php判断字段在不在再输出js代码。
另外一个页面只放一个ID,你的ID要用test,应该命名为 "test+随机数(一个独立的数字母什么一连串有特征的就行)"。
蠢办法就是while里每输出一个文章就输出一段js。

哦哦,好的,谢谢大佬了 :D
回复