简体   繁体   中英

How could I add the same string on different paragraph multiple time on the same HTML page?

I wish to know the best way to write only once the same thing and repeat inside the same page. For example:

<div>
    <p id="description1"></p>
</div>
<div>
    <p id="description1"></p>
</div>
--

I wish to write only one time the description1 inside the body . I think this could be achieved using the DOM.

Put the elements in the same class using the class attribute, then get the list of all elements using the getElementsByClassName() DOM function. You can then go over the list using a for loop .

[].forEach.call(document.getElementsByClassName("description"), function(elem) {
        elem.innerHTML = "StackOverflow saved my day!";
});

You can even put the text in all elements of the same class using no JavaScript and only CSS by using the content attribute.

First of all, the ID field should be unique per element.

If you give all the tags a class <p class="description"></p> then you can use jQuery to set them all by calling:

$('.description').text('This is the text')

In javascript:

var elements = document.getElementsByClassName("description");
for (var i = 0; i < elements.length; i++) {
    elements[i].innerHTML = "This is the text.";
}

Have a look at the solutions proposed here How to repeat div using jQuery or JavaScript?

this one seems to work pretty well:

html:

<div id="container">data</div>

js:

var container = document.getElementById('container');

function block(mClass, html) {
    //extra html you want to store.
    return '<div class="' + mClass + '">' + html + '</div>';
}

//    code that loops and makes the blocks.
//    first part: creates var i
//    second:     condition, if 'i' is still smaller than three, then loop.
//    third part: increment i by 1;
for (var i = 0; i < 3; i++) {
    // append the result of function 'block()' to the innerHTML
    // of the container.
    container.innerHTML += block('block', 'data');
}

JSFIDDLE

two elements cannot have same id but can have same class

 <head>
        <script>
            var x = document.getElementsByClassName("description");
            for (var i = 0; i < x.length; i++) {
                    x[i].innerHTML = "This is the text.";
            }
        </script>

        <style>
               .description1 {                    // this will apply the same style to all elements having class as description1
                  text-align: center;
                  color: red;
               }
        </style>
 </head>

    <body>
        <div>
            <p class="description1"></p>
        </div>
        <div>
            <p class="description1"></p>
        </div>
    </body>

See the script tag. this solves your problem

Just added with a code by using

getElementsByClassName()

`<html>
<body>

<div class="example">First div element with class="example".</div>

<p class="example">Second paragraph element with class="example".</p>

<p>Click the button to change the text of the first div element with class="example" (index 0).</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> The getElementsByClassName() method is not supported in Internet Explorer 8 and earlier versions.</p>

<script>
function myFunction() {
    var x = document.getElementsByClassName("example");
    for(var i=0;i< x.length;i++)
    x[i].innerHTML = "Hello World!";
}
</script>

</body>
</html>`

If you wish to keep id, change your code like this :

script :

var pcount = 2// # p
var desc = document.getElementById('description1');
for(i=0; i<pcount;i++){
   document.getElementById('description' + i).innerHTML = desc;
}

html

<div>
 <p id="description1"></p>
</div>
<div>
 <p id="description2"></p>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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