简体   繁体   English

如何在新行上使用内部div拆分或获取div的内容

[英]how to split or get contents of div with inner div separate on new line

I have this code: 我有以下代码:

 <div id="nlp_selected_day"> <div class="nlp_months nlp_selected_month_calendar_shown" style="display: inline-block;"><div class="nlp_day_selected_dummy_day"></div> <div class="nlp_day_selected_dummy_day"></div> <div class="nlp_day_selected_dummy_day"></div> <div id="1" class="nlp_day_selected nlp_thursday" value="1" title="Четвъртък"> 1 <div></div></div> <div id="2" class="nlp_day_selected nlp_friday" value="2" title="Петък"> 2 <div class="calendar_day_container"></div></div> ...... <div id="5" class="nlp_day_selected nlp_monday" value="5" title="Понеделник"> 5 <div class="calendar_day_container"> <div>8:00 - 8:45 </div> <div class="hidden_info"> <div class="hidden_title">Запазено!</div> <div class="hidden_description"><br></div> </div> <div>8:45 - 9:30 </div> <div class="hidden_info"> <div class="hidden_title">Запазено!</div> <div class="hidden_description"><br></div> </div> <div>9:40 - 10:25 </div> <div class="hidden_info"> <div class="hidden_title">Запазено!</div> <div class="hidden_description"><br></div> </div> <div>10:25 - 11:10 </div> <div class="hidden_info"> <div class="hidden_title">Запазено!</div> <div class="hidden_description"><br></div> </div> <div>11:20 - 12:05 </div> ....... <div id="6" class="nlp_day_selected nlp_tuesday" value="6" title="Вторник"> 6 <div class="calendar_day_container"> <div>8:00 - 8:45 </div> <div class="hidden_info"> <div class="hidden_title">Запазено!</div> <div class="hidden_description"><br></div> </div> <div>8:45 - 9:30 </div> <div class="hidden_info"> <div class="hidden_title">Запазено!</div> <div class="hidden_description"><br></div> ...... 

I get content with this code: 我对以下代码感到满意:

 jQuery('.nlp_day_selected').click(function (){ var contents = $(this).children('.calendar_day_container').text(); $('#nlp_show_content').html(contents); }); 

but I want to get content divided into a new line each time block 但我想在每个时间段将内容分成新行

something like this: 像这样的东西:

14:40 - 15:25 100 минути descriptions 14:40-15:25 100мминути说明

15:25 - 16:10 title descriptions 15:25-16:10标题说明

16:20 - 17:05 title descriptions 16:20-17:05标题说明

While I believe your question could have been phrased better, I will still make an attempt to answer your question. 尽管我相信您的问题本来可以用更好的措词来表达,但我仍会尝试回答您的问题。

First of all, it could be your formatting, but I think you have some </div> tags that have no opening <div> tags, you should check to see if that is an actual error or not (you might have copy pasted wrong who knows). 首先,可能是您的格式,但是我认为您有一些</div>标签,这些标签没有打开的<div>标签,您应该检查一下这是否是实际错误(您可能粘贴了错误的复制内容)谁知道)。

Furthermore, I think you should make a few small changes to your code to make it a bit easier to get at the data you want. 此外,我认为您应该对代码进行一些小的更改,以使其更容易获得所需的数据。

You should change all code that looks like this: 您应该更改所有看起来像这样的代码:

<div>10:25 - 11:10 </div>
   <div class="hidden_info">
   <div class="hidden_title">Запазено!</div>
   <div class="hidden_description"><br></div>

To something like this: 对于这样的事情:

<div class="timeslot_container">   
   <div class="timeslot">10:25 - 11:10 </div>
   <div class="hidden_info">
   <div class="hidden_title">Запазено!</div>
   <div class="hidden_description"><br></div>
</div>

And then, you can do something like this using jQuery (modify it to fit your needs): 然后,您可以使用jQuery执行类似的操作(根据您的需要对其进行修改):

var contents = $("<div></div>");
$('.calendar_day_container').each(function (i, elt) {
    var $htmlForOneDay = $("<div></div>");
    $(elt).find(".timeslot_container").each(function (i, elt) {
        var $singleTimeslot = $("<div></div>");
        $singleTimeslot.append($(elt).find(".timeslot").text()).append(" ");
        $singleTimeslot.append($(elt).find(".hidden_title").text()).append(" ");
        $singleTimeslot.append($(elt).find(".hidden_description").text());
        $htmlForOneDay.append($singleTimeslot).append($("<br/>"));
    });
    contents.append($htmlForOneDay).append("<hr/>"); // you can skip the <hr/> if you want
});
$('#nlp_show_content').html(contents[0]);

I believe this will get you the data in the format you want, or at least lead you in the right direction, I hope this helps you :) 我相信这将为您提供所需格式的数据,或者至少将您引向正确的方向,希望对您有所帮助:)

Loop over all of the hidden_info class and use those instances to create each line: 遍历所有hidden_info类,并使用这些实例创建每一行:

jQuery('.nlp_day_selected').click(function () {
    var htmlString = '';
    var $hidden = $(this).find('.hidden_info');
    $hidden.each(function () {
        var $el = $(this),
            time = $el.prev().text(),// time is in previous element
            title = $el.find('.hidden_title').text(),// title is a child
            description = $el.find('.hidden_description').text();// description is a child

        htmlString += time + ' ' + title + ' ' + description + '<br>';
    });    

    $('#nlp_show_content').html(htmlString);
});

Not you have full control over each line spacing and formatting 不是您完全控制每行的间距和格式

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

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