简体   繁体   English

动态生成的内容DIV

[英]Dynamically Generated Contents DIV

I have seen the bootstrap github page and how they process their sidebar that slides down and has all the titles of the articles. 我已经看到了Bootstrap github页面,以及它们如何处理其侧栏,该侧栏向下滑动并具有文章的所有标题。

I am trying to think of a way that would build upon, the current system I have which is manually typing out each anchor point and creating a link for it. 我正在尝试一种可以在现有系统上建立的方式,该系统可以手动键入每个锚点并为其创建链接。

Currently I have 目前我有

<h1>Main Title</h1>
<a href="#Para5" class="scroll">Para5</a>
<a href="#Para10" class="scroll">Para10</a>
<a href="#Para15" class="scroll">Para15</a>
<a href="#Para20" class="scroll">Para20</a>
<br/>
<h2>Para1</h2>
     <p>.....</p>
<h2>Para2</h2>
     <p>.....</p>
<h2>Para3</h2>
     <p>.....</p>
<h2>Para4</h2>
     <p>.....</p>
<h2>Para5</h2>
     <p>.....</p>
etc...

I then have to manually add the id's for such headings, aiming to have every 5 headings have a link and a button that says "Show all" that would show all of the headings for the page. 然后,我必须手动添加此类标题的ID,以使每5个标题都有一个链接和一个显示“ Show all”(显示全部)的按钮,以显示该页面的所有标题。

When creating a page I which gets typed up using a HTML embedded text editor so code is invisible and stored in MySQL database. 创建页面I时,使用HTML嵌入式文本编辑器输入页面I,因此代码是不可见的,并存储在MySQL数据库中。

I would just like to have the headings and paragraphs and then the Main Page title, typed out but to have the links dynamically insert themselves into the page, underneath the main title. 我只想输入标题和段落,然后输入Main Page标题,但是要让链接动态地将其自身插入到页面的主标题下。

  1. Capture text between h2 and /h2, create a link and ID to it. 在h2和/ h2之间捕获文本,创建指向它的链接和ID。 Do this for all h2 on the page. 对页面上的所有h2执行此操作。

I cannot for the life of me think how to capture information like this out of a page being created by PHP. 我一生都无法想到如何从PHP创建的页面中捕获此类信息。

The processing of the page could be done in post with jQuery and Javascript or pre in PHP. 该页面的处理可以在jQuery和Javascript之后或在PHP中完成。

Prefer to do things in PHP. 首选在PHP中执行操作。


Ignoring the 5th element thing. 忽略第五要素。

<h1>Main Title</h1>
<div id="paraNav">
</div>
<br/>
<div id="headings">
<h2>Para1</h2>
     <p>.....</p>
<h2>Para2</h2>
     <p>.....</p>
<h2>Para3</h2>
     <p>.....</p>
<h2>Para4</h2>
     <p>.....</p>
<h2>Para5</h2>
     <p>.....</p>
</div>

Aim is to grab the text of the heading so something like: 目的是获取标题文本,例如:

$('#headings h2').each(function(i,$el){
    var headingTitle = $el.text;  **  
      document.write('<a href="#headingTitle" class="scroll">headingTitle</a>')
    $('h2').append.attr({
         'id':headingTitle
    });
});

**not sure if this bit exists. **不确定此位是否存在。 (get content/value/text between tags) (获取标签之间的内容/值/文本)

You can likely do this with jQuery. 您可能可以使用jQuery完成此操作。

This is quick and dirty, but can probably get you started. 这既快速又肮脏,但是可能可以帮助您入门。 I haven't tested this. 我还没有测试。 There may be syntax errors. 可能存在语法错误。

var linkArray = [];

$('h2:nth-child(5)').each(function () {
    linkArray.push({
        text: $(this).text,
        // href: $(this).id
    });
});

for (var i = 0; i < linkArray.length; i++) {
    $('#linkcontainer').append('<a href="' + linkArray[i].text + '">' + linkArray[i].text + '</a>');
}

I think jQuery has some better tools for doing what you want to do. 我认为jQuery有一些更好的工具可以做您想做的事情。 It might be a sloppy job if you tried to do it with PHP, whereas jQuery you can do it with a few lines. 如果您尝试使用PHP来完成这项工作可能是一项草率的工作,而jQuery则只需几行就可以完成。 Something like 就像是

$('#content h2:nth-of-type(5n)').each(function(i,$el){
    ver headingId = $el.attr('id');
    $('#contentNav').append(
        $('<a/>').html(headingId).attr({
            'href':'#'+headingId,
            'class':'scroll'
        })
    );
});

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

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