用30个符号更新每个h1标签(h1标签下面3个段落中的10个符号)

时间:2016-12-09 11:57:20

标签: javascript html for-loop dom

所以我有12个段落,这些段落被三个p标签分成四组。我需要做的是每三个段落得到10个符号(总共30个符号,包括空格)并将其联系人添加到h1标签,需要做4次,因为有3个组有12个段落。

 <div id="pastraipos">
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>it is a long established fact that a reader will be distracted.</p>
 <p>There are many variations of passages of Lorem Ipsum available.</p>
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 </div>

html代码中没有h1标签,我只是为你写的,所以它可以更清楚,因为我很难解释它。 所以有代码,我每隔3段添加一次h1标签: -

var parent = document.getElementById("pastraipos");
var children = parent.childElementCount;
console.log(children);
for (var i=0; i<=children; i=i+4){
var h = document.createElement("H1"); 
var t = document.createTextNode("Hello World"); //there should be no hello world it should contain 30 symbols from three paragraphs (10 from each including space)    
h.appendChild(t);
parent.insertBefore(h, parent.children[i]);    
}

这是我编写的代码,用于获取每三段中的10个符号

 for (j=0; j<3; j++){
 var str = document.getElementsByTagName("p")[j].textContent;
 console.log(str);
 var res = str.substring(0, 10);
 console.log(res); 
    var labas = labas + res;
 }
 var s = document.createElement("H1"); 
 var t = document.createTextNode(labas);    
 s.appendChild(t);
 parent.appendChild(s);

这应该出现在第一个h1

<h1>is simply it is a lot here are </h1>(30 elements total)

所以我有代码,每3段添加h1标签,我有一些代码,从3段收集10个符号并将文本添加到h1,遗憾的是它只适用于第一个h1,因为不知何故我需要结合这两个循环?但我不知道如何。

2 个答案:

答案 0 :(得分:2)

H_ello f_riend,我不太确定我是否完全理解你,但你可以查看这段代码是否会对你有所帮助 - &gt; http://codepen.io/anon/pen/BQPygr?editors=1010

HTML:

<button onclick="doWork()">Run Function</button>
<button onclick="clearH1()">Reset</button>

<div id="pastraipos">
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>it is a long established fact that a reader will be distracted.</p>
 <p>There are many variations of passages of Lorem Ipsum available.</p>
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <h1></h1>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 </div>

JS:

function doWork() {
  /* get all elements from the parent div */
  var children = document.getElementById('pastraipos').childNodes;
  var childrenLen = children.length;
  var childrenText = '';

  /* reverse loop */
  for (var i = children.length - 1; i >= 0; i--) {
    if (children[i].tagName === 'H1') {
      /* if element is <h1> set collected text */
      children[i].innerHTML = childrenText;
      childrenText = '';
    } else {
      /* assume element is <p> then get first 10 characters */
      childrenText += children[i].textContent.substr(0, 10);
    }
  }
}

function clearH1(){
  var children = document.getElementById('pastraipos').childNodes;
  for (var i = children.length - 1; i >= 0; i--) {
    if (children[i].tagName === 'H1') {
      children[i].innerHTML = '';
    }
  }
}

<强>更新

HTML:

<button onclick="doWork()">Run Function</button>
<button onclick="clearH1()">Reset</button>

<div id="pastraipos">
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>it is a long established fact that a reader will be distracted.</p>
 <p>There are many variations of passages of Lorem Ipsum available.</p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 <p>is simply dummy text of the printing and typesetting industry. </p>
 </div>

JS:

Element.prototype.remove = function() {
  this.parentElement.removeChild(this);
}

function doWork() {
  var parent = document.getElementById('pastraipos');
  var children = parent.childNodes;
  var childrenLen = children.length;
  var childrenText = '';
  var counter = 0;
  for (var i = children.length - 1; i >= 0; i--) {
    if (counter === 3) {
      var h1Element = document.createElement('h1');
      h1Element.appendChild(document.createTextNode(childrenText));
      parent.insertBefore(h1Element, children[i]);
      childrenText = '';
      counter = 0;
    } else if(children[i].tagName === 'P') {
      childrenText += children[i].textContent.substr(0, 10);
      counter++;
    }

  }
}

function clearH1() {
  var children = document.getElementById('pastraipos').childNodes;
  for (var i = children.length - 1; i >= 0; i--) {
    if (children[i].tagName === 'H1') {
      children[i].remove();
    }
  }
}

答案 1 :(得分:1)

  

必须只使用js和dom,不幸的是没有css

这是两个解决方案。一个使用javascript,一个使用css。该脚本在每个第三段(p)之后创建一个标题(h1)标记,并显示其中每个前面段落中的前十个字符:

<html>
    <head>
        <script>
            //Creating dummy data
            function createDummyData(){
                var tF = document.createDocumentFragment();
                for(var i=0, j=100; i<j; i++){
                    var tP = tF.appendChild(document.createElement('p'));
                    tP.innerHTML = 'Line' + i + ': and some more characters'
                };

                document.querySelector('div').appendChild(tF)
            }

            window.onload = function(){
                createDummyData(); //Creating summy data

                //Looping though all the paragraphs
                for(var tA=[], tL=document.querySelectorAll('.Blubb p'), i=0, j=tL.length; i<j; i++){
                    //Store the first ten characters
                    tA.push(tL[i].textContent.substr(0, 10));

                    //Each tenths step..
                    if((i + 1)%3 === 0){
                        //A header gets inserted
                        var tH = document.createElement('h1');
                        tH.innerHTML = tA.join('.. ');
                        tL[i].parentNode.insertBefore(tH, tL[i].nextSibling);
                        tA = [] //Empty the characters
                    }
                }
            }
        </script>

        <style>
            /* Alternativly this is a simple solution by merely using css, here marking each third element */
            .Blubb p:nth-of-type(3n+3):after{
                content: 'thid element reached by css';
                color: red
            }
        </style>
    </head>

    <body>
        <div class = 'Blubb'>
            <!-- Created by dummy -->
        </div>
    </body>
</html>

https://jsfiddle.net/9shfo5ge/2/