【问题标题】:Load each clicked post into a page将每个点击的帖子加载到页面中
【发布时间】:2013-09-28 12:45:31
【问题描述】:

我有以下代码工作,但希望当我点击每个帖子时,帖子内容将使用外部 .html 或在默认 html 中显示 div 页面,以便内容不会显示整个网站内容:

HTML 代码:

<head>
    <meta charset="utf-8">
    <title>Hope</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />

   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <script src="css/style.css"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    <script src="js/script.js"></script>

</head>
<body>

    <div id="devotionpage" data-role="page">
    <div data-role="header" class="sys_hd" data-position="fixed" data-id="sys_header" data-theme="c" >
        <h1>Daily Devotional Messages</h1>
        </div><!-- header -->

        <div data-theme="c" data-role="content" id="devotionlist"> </div><!-- content -->

        <div data-role="footer" data-position="fixed" data-id="sys_footer" data-theme="c">
                    <div data-role="navbar" >
                <ul>
                    <li><a href="#devotionpage" class="sys_ft">Home</a></li>
                    <li><a href="#devotionpage" class="sys_ft">Disclaimer</a></li>
                </ul>
            </div><!-- navbar --> 
        </div><!-- footer --> 
    </div><!-- page -->


    <div id="articlepost" data-role="page" data-transition="fade">
    <div data-role="header" class="devotion_hd" data-position="fixed" data-theme="c" >

        <div data-theme="c" data-role="content" id="articlecontent"> </div><!-- content -->

        <div data-role="footer" data-position="fixed" data-id="sys_footer" >
                    <div data-role="navbar" >
                <ul>
                    <li><a href="#devotionpage" class="sys_ft">Home</a></li>
                    <li><a href="#devotionpage" class="sys_ft">Disclaimer</a></li>
                </ul>
            </div><!-- navbar --> 
        </div><!-- footer --> 
    </div><!-- page -->

</body>
</html>

JS代码:

$(document).on("pagebeforeshow", "#devotionpage", function() {
    $(this).find(".ui-listview-filter input").val("").trigger("change");
    });

$( document).ready(function (){
    var url = 'http://howtodeployit.com/category/daily-devotion/feed/?json=recentstories&callback=listPosts' ;
    $.ajax({
        type: "GET",
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        error: function(){
            alert( 'Unable to load feed, Incorrect path or invalid feed' );
        },
        success: function(data ){
            var postlist = data.responseData.feed.entries;
            var html = '<ul data-role="listview" data-filter="true">' ;
            for (var i = 0 ; i < 6 ; i++) {
                var entry = postlist[i];
                html += '<li>';
                html += '<a href="' + entry.link + '">';
                html += '<div class="etitle">' + entry.title + '</div>' ;
                html += '<div class="eauthor">' + entry.author + '</div>' ;
                html += '<div class="epubdate">' + entry.publishedDate + '</div>';
                html += '<div class="esnippet">' + entry.contentSnippet + '</div>';
                html += '</a>';
                html += '</li>';
            }
            html += '</ul>';
           $( "#devotionlist" ).append(html);
           $( "#devotionlist ul[data-role=listview]" ).listview();

        }});
    });

谢谢

【问题讨论】:

  • 您想在不同的页面中显示结果?
  • 是的@Omar,我的目的是在单独的页面中显示每个帖子内容。

标签: javascript php jquery jquery-mobile


【解决方案1】:

我发现代码存在一个问题。函数 showarticle 有这一行

(html+='</ul>';) outside the ajax call's callback handler, but the variable html is declared inside it. So, actually the line 

html+='</ul>' is translated to undefined += '</ul>', which is not correct, unless you have var html; declared globally outside.

【讨论】:

  • 嗨,Rajesh,我重新表述了我的问题,以便每个人都能理解
【解决方案2】:

"要使用外部 .html 显示的帖子内容"

您可以对内容类型为 text/html 的外部 html 进行 ajax 调用,并将响应加载到 div 中。

【讨论】:

  • 为了其他人学习和理解,你能给我一个可行的代码来实现这一点。谢谢
【解决方案3】:

恐怕我不应该发布完整的可行代码,因为 SO 是为了帮助人们实现他们的解决方案,并为他们指明正确的方向。所以,我只贴一个代码sn-p。

$.ajax({
  url: your_server_side_script.extension, //example: handleInput.php
  dataType: 'text/html',
  data: {postid: $('#postid').val()},
  success: function(responseHTML) {
     $('#postContentDiv').html(responseHTML);
  }
});

your_server_side_script.extension (handleInput.php) 应该生成 html 标签并打印出来,以便在 ajax 成功时可以在 responseHTML 变量中使用。

【讨论】:

  • 已经在 meta.官方很好,鼓励给出完整的答案。
  • 感谢 AlKid 提供这个有用的更新。该网站旨在帮助彼此和即将到来的程序员
【解决方案4】:

我创建了一个函数来处理每个帖子在不同页面上的显示:

function showPost(id) {
$('#devotionlist').html("Please wait...");
$.getJSON('http://howtodeployit.com/?json=get_post&post_id=' + id + '&callback=?', function(data) {
    var output='';
    html += '<h3>' + data.post.title + '</h3>';
    html += data.post.content;
    $('#devotionlist').html(html);
}); //get JSON Data for Stories
} //showPost

这个位调用onClick函数:

html += '<a href="#articlecontent" onclick="showPost(' + val.id + ')">';

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 2023-02-05
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2016-05-14
    相关资源
    最近更新 更多