在jquery mobile制作app的几个页利用ajax将后台数据json数组动态加载到列表里面

后来查找资料,发现新增节点后虽然数据加载上去了,但是样式却没有加载上,因此在整个过程中有2个注意事项。

1、利用循环将json动态加到html中

2、动态刷新每个节点的样式。

server.php

<?php
//todo:获取数据,转换为json
// try{
//     $pdo=new PDO('mysql:host=localhost;dbname=','root','playboy');
//     $sql="select * from user where username=? and password=?";
//     $stmt=$pdo->prepare($sql);
//     $stmt->execute(array($username,$password));
//     echo $stmt->rowCount();

// }catch(PDOException $e){
//     echo $e->getMessage();
// }
//  $pdo=new PDO('mysql:host=localhost;dbname= test_jilv','root','');
$result = array();
try{    
$pdo=new PDO('mysql:host=localhost;dbname=test_jilv','root','playboy');
$pdo->query("set names utf8");

        $sql="select * from my_activity order by id desc";

        $stmt=$pdo->prepare($sql);

        $stmt->execute();

        while($row=$stmt->fetch()){

            $data['title']=$row['title'];
            $data['address']=$row['address'];
            $data['time']=$row['time'];
            //echo json_encode($data);
            $result[] = $data;
            //...
            //...

        }
}catch(PDOException $e){
    echo $e->getMessage();
}
echo json_encode($result);
//echo json_encode($data);

function toUtf($value){
    if($value){
        return icon_to_utf8($value);
    }else{
        return false;
    }
}
?>

2.index,html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewpoint" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.2/jquery.mobile.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
<style>
.ui-bar-f
{
color:#30b4ed;
background-color:#30b4ed;
}
.ui-body-f
{
font-weight:bold;
color:#30b4ed;
}
#img{
    margin-top:11px;
    margin-left:3px;
    border-radius:0;
}
</style>
</head>
<body>
<script type="text/javascript">
            $("document").ready(function() {
                var url ="server.php";
                var data={};
                $.getJSON(url,data,function(res){
                            var htmlStr='';
                            for (var i =0; i<res.length; i++) {
                      var o = res[i];
                      htmlStr += '<li>'
                              +  '  <a href="activity_detail.html" data-transition="flip">'
                              +  '    <img src="RAW.PNG"/>'
                              +  '    <h2 id="" name="">' + o['title'] +'</h2>'
                              +  '    <p id="" name="">' + o['address'] +'</p>'
                              +  '    <p id="" name="">' + o['time'] +'</p>'
                              +  '  </a>'
                              +  '</li>';
                    
                    }
                      $('#list').append(htmlStr);
                      $('ul').listview('refresh');        //将节点样式刷新
                    });
              });
        </script>
<div data-role="page" >
    <div data-role="header" data-position="fixed" data-theme="f">
        <h1></h1>
    </div>
        <ul id="list"data-role="listview" data-inset="true">        <!--不使用内容栏,直接在页面中加入列表-->
        
    </ul>
</div>
</body>
</html>

115112_9xCI_1778933.png

转载于:https://www.cnblogs.com/hcxl/p/8321590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值