简体   繁体   English

嵌套的foreach jquery孩子

[英]Nested foreach jquery children

<div class="right_channel_bar" id="channel_box" style="margin-left: -12px;">
    <div class="row" id="channel_1">
        <span class="program" data-start-time="00:00" data-end-time="00:30" style="margin-left: 0px">
            <a class="truncate" style="width: 180px; href=" javascript:void(0); "=" ">bing technology network</a></span><span class="program " data-start-time="00:31 " data-end-time="01:00 " style="margin-left: 6px "><a class="truncate " style="width: 174px; href=" javascript:void(0);"="">songs</a>
        </span>
        <span class="program" data-start-time="01:02" data-end-time="02:00" style="margin-left: 12px">
            <a class="truncate" style="width: 348px; href=" javascript:void(0); "=" ">news channel</a></span><span class="program " data-start-time="02:05 " data-end-time="03:00 " style="margin-left: 30px "><a class="truncate " style="width: 330px; href=" javascript:void(0);"="">evenst</a>
        </span>
        <span class="program" data-start-time="03:00" data-end-time="04:00" style="margin-left: 0px">
            <a class="truncate" style="width: 360px; href=" javascript:void(0); "=" ">microsoft technology network</a></span><span class="program " data-start-time="04:00 " data-end-time="04:45 " style="margin-left: 0px "><a class="truncate " style="width: 270px; href=" javascript:void(0);"="">yahoo technology network</a>
        </span>
    </div>
    <div class="row" id="channel_2">
        <span class="program" data-start-time="00:00" data-end-time="00:30" style="margin-left: 0px">
            <a class="truncate" style="width: 180px; href=" javascript:void(0); "=" ">bing technology network</a></span><span class="program " data-start-time="00:31 " data-end-time="01:00 " style="margin-left: 6px "><a class="truncate " style="width: 174px; href=" javascript:void(0);"="">songs</a>
        </span>
        <span class="program" data-start-time="01:02" data-end-time="02:00" style="margin-left: 12px">
            <a class="truncate" style="width: 348px; href=" javascript:void(0); "=" ">news channel</a></span><span class="program " data-start-time="02:05 " data-end-time="03:00 " style="margin-left: 30px "><a class="truncate " style="width: 330px; href=" javascript:void(0);"="">evenst</a>
        </span>
        <span class="program" data-start-time="03:00" data-end-time="04:00" style="margin-left: 0px">
            <a class="truncate" style="width: 360px; href=" javascript:void(0); "=" ">microsoft technology network</a></span><span class="program " data-start-time="04:00 " data-end-time="04:45 " style="margin-left: 0px "><a class="truncate " style="width: 270px; href=" javascript:void(0);"="">yahoo technology network</a>
        </span>
    </div>
</div>

From the above mentioned HTML, i need to get data-start-time but i am unable to fetch. 从上面提到的HTML,我需要获取data-start-time但我无法获取。 Kindly help me fix the jquery code below: 请帮我修复下面的jquery代码:

$('#channel_box .row').each(function () {
    $(this).children(".program").each(function () {
        console.log($(this).data('start-time') + ' - ' + $(this).data('start-time'))
    });
});

试试看:

console.log($(this).attr('data-start-time') + ' - ' + $(this).attr('data-start-time'))

Your jQuery code works fine, it is your markup that is the problem. 你的jQuery代码工作正常,这是你的标记问题。

I believe it's the unescaped quotes at this point: 我相信这是在这一点上未转义的引号:

javascript:void(0);"="">b

If you format your HTML correctly as shown below (And also in the JSFiddle) it will work fine. 如果您正确格式化HTML ,如下所示(也在JSFiddle中),它将正常工作。

<div class="right_channel_bar" id="channel_box" style="margin-left: -12px;">
   <div class="row" id="channel_1">
      <span class="program" data-start-time="00:00" data-end-time="00:30" style="margin-left: 0px"></span>
      <span class="program" data-start-time="00:01" data-end-time="00:30" style="margin-left: 0px"></span>
   </div>
   <div class="row" id="channel_2">
      <span class="program" data-start-time="00:00" data-end-time="00:30" style="margin-left: 0px"></span>
      <span class="program" data-start-time="00:01" data-end-time="00:30" style="margin-left: 0px"></span>
   </div>
</div>

http://jsfiddle.net/vx7vv6tm/1/ http://jsfiddle.net/vx7vv6tm/1/

Also you may want to consider using a CSS class and remove your inline styling. 您也可以考虑使用CSS类并删除内联样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM