jquery - 使用 jquery nextUntil 存储时间数组并显示电视节目

标签 jquery ajax

我想要抓取一个具有以下格式的页面:

<ul>
<li>7 pm</li>
<li>8 pm</li>
<ul>

<h6 id="7 pm">7 pm</h6>
<div class="show">My TV Show #1</div>
<div class="show">My TV Show #2</div>
<div class="show">My TV Show #3</div>
<h6 id="8 pm">8 pm</h6>

如何让它显示时间,然后显示该时间的所有节目?

我曾尝试成功地将时间存储在任何数组中,但当涉及到实际对时间数组执行某些操作时,我遇到了困难。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>jQuery PHP Page Scape</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script type="text/javascript">
        $("document").ready(function() {
            var times = new Array();

            $.ajax({
                url: "curl.php",
                dataType: 'text',
                success: function(data) {
                    $(data).find('.wnt-top-r ul li').each(function(i, item) {
                        times.push($(item).text());
                        $('#content').append(times[i]);
                    });
                }
            });
        });
    </script>
</head>
<body>

    <div id="content"></div>

</body>
</html>

谢谢, 布赖恩

最佳答案

更改您的标记。对于布局的完成方式来说,这是不可能的/优化的。

首先,有效的 HTML ID 名称必须以字母开头。

第二,只能有1个相同的ID,不能重复。

第三,ID 名称中的空格不是有效字符。

由于您的标记无效,因此提出建议。

<div id="container">
    <ul>
        <li data-showtime="19">7pm</li>
        <li data-showtime="20">8pm</li>
        <li data-showtime="21">9pm</li>
    </ul>

    <h6 class="time-title time-19" data-showtime="19">7pm</h6>
    <div class="show time-19">My TV Show 1</div>
    <div class="show time-19">My TV Show 2</div>
    <div class="show time-19">My TV Show 3</div>
    <div class="show time-19">My TV Show 4</div>
    <div class="show time-19">My TV Show 5</div>
    <h6 class="time-title time-20" data-showtime="20">8pm</h6>
    <div class="show time-20">My TV Show 5</div>
    <div class="show time-20">My TV Show 6</div>
</div>

​ 和脚本:

  $(function() {
        showtime = {};
        $("#container").find("h6.time-title").each(function() {
            var t = $(this);
            var d = t.data();
            showtime[d.showtime] = {};
            showtime[d.showtime]['caption'] = t.text();

            var tempshows = [];
            $(".show.time-"+d.showtime).each(function() {
                showname = $(this).text();
                tempshows.push(showname)
            })

            showtime[d.showtime]['shows'] = tempshows;
        })

console.log(showtime);

    })
​

jsFiddle:http://jsfiddle.net/FhKAh/1/

如果您无法更改标记并且必须处理此问题,请尝试以下操作:

showtime = {};
$("h6").each(function() {
    h6 = $(this);

    var tempshow = [];
    h6.nextUntil("h6").each(function() {
        tempshow.push($(this).text())
    });


    showtime[h6.text()] = {};
    showtime[h6.text()]['shows'] = tempshow;


});

    console.log(showtime);

jsFiddle:http://jsfiddle.net/CGyBK/

关于jquery - 使用 jquery nextUntil 存储时间数组并显示电视节目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328201/

相关文章:

javascript - 点击事件中的 jQuery 函数

javascript - ajax POST数据结果在控制台,却显示在页面上?

jquery - 如何通过 JSON 或 HTTP 代理访问 Rally REST API JSON 响应?

jquery - AJAX + SEO 快速提问

jQuery 插件 Lightbox 横条不显示

javascript - jQuery 如何使用从单击的 div 中获取的 ID

javascript - 如何将 JavaScript 监听器添加到 PrimeFaces Ajax 事件

javascript - 使用 AJAX 和 jQuery .get() 从外部文档检索特定元素

PHP - 仅在主页上隐藏元素 - WordPress 网站

javascript - Ajax Complete 在成功准备好之前被触发,如何防止这种行为?