Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在同一HTML页面的不同段落多次添加相同的字符串?_Javascript_Html - Fatal编程技术网

Javascript 如何在同一HTML页面的不同段落多次添加相同的字符串?

Javascript 如何在同一HTML页面的不同段落多次添加相同的字符串?,javascript,html,Javascript,Html,我想知道最好的方法,只写一次同样的东西,并在同一页内重复。例如: <div> <p id="description1"></p> </div> <div> <p id="description1"></p> </div> -- -- 我只希望在正文中写入一次说明1。我认为这可以通过使用DOM来实现。首先,每个元素的ID字段应该是唯一的 如果为所有标记指定一个class,则可以

我想知道最好的方法,只写一次同样的东西,并在同一页内重复。例如:

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

--

我只希望在
正文
中写入一次
说明1
。我认为这可以通过使用DOM来实现。

首先,每个元素的ID字段应该是唯一的

如果为所有标记指定一个class

,则可以使用jQuery调用以下命令来设置所有标记:

在javascript中:

var elements=document.getElementsByClassName(“说明”);
对于(var i=0;i
使用
class
属性将元素放在同一个类中,然后使用DOM函数获取所有元素的列表。然后,您可以使用


您甚至可以使用
content
属性,不使用JavaScript,只使用CSS将文本放入同一类的所有元素中。

两个元素不能具有相同id,但可以具有相同类

 <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>

var x=document.getElementsByClassName(“说明”);
对于(变量i=0;i


查看脚本标记。这就解决了您的问题

刚刚通过使用

getElementsByClassName()

`
带有class=“example”的第一个div元素。

带有class=“example”的第二段元素

单击该按钮可使用class=“example”(索引0)更改第一个div元素的文本

试试看 注意:Internet Explorer 8和早期版本不支持getElementsByClassName()方法

函数myFunction(){ var x=document.getElementsByClassName(“示例”); 对于(变量i=0;i
看看这里提出的解决方案

这一个似乎工作得很好:

html:

数据
js:

var container=document.getElementById('container');
功能块(mClass,html){
//要存储的额外html。
返回“”+html+“”;
}
//循环并生成块的代码。
//第一部分:创建var i
//第二:条件,如果“i”仍然小于3,则循环。
//第三部分:增量i乘以1;
对于(变量i=0;i<3;i++){
//将函数“block()”的结果附加到innerHTML
//容器的顶部。
container.innerHTML+=block('block','data');
}

如果您希望保留id,请按以下方式更改代码:

脚本:

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

对于(i=0;i可能他没有使用jquery…:-)如果我有2个类,它是否相同?是的,它将更新共享相同类名的所有元素@neuhaus True,True…由于GetElementsByCassName()返回一个列表,我认为您不能像那样使用innerHTML-您测试过吗?@neuhaus我没有。感谢您指出,我以前很少使用纯javascript@Simone是的,如果你指的是类似于
的东西,那么它将具有红色框样式,文本也将更新。给它们相同的类和不同的ID。然后,正如neuhaus所回答的那样,使用GetElementsByCassName来控制它。这可能不是答案。grazie Simone,这是一个非常好的解决方案,所以基本上我把这个
改成了(var I=0;I<0;I++){
@Simone似乎有打字错误(var I=0;I<3;I++)…not
i<0
:)@我用JSFIDLE iI尝试的根目录不认为您可以在
节点列表上调用
forEach
-解决这个问题的简单方法是使用
[].forEach.call(document.getElementsByClassName(“description”),function(){…}
很高兴知道,我修复了它。我从控制台
未捕获的语法错误:在
函数(elem){
[].forEach.call(document.getElementsByClassName("description"), function(elem) {
        elem.innerHTML = "StackOverflow saved my day!";
});
 <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>
`<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>`
<div id="container">data</div>
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');
}
var pcount = 2// # p
var desc = document.getElementById('description1');
for(i=0; i<pcount;i++){
   document.getElementById('description' + i).innerHTML = desc;
}
<div>
 <p id="description1"></p>
</div>
<div>
 <p id="description2"></p>
</div>