问题:
I want to replace the contents within a html element so I'm using the following function for that:我想替换 html 元素中的内容,因此我为此使用了以下函数:
function ReplaceContentInContainer(id,content) {
var container = document.getElementById(id);
container.innerHTML = content;
}
ReplaceContentInContainer('box','This is the replacement text');
<div id='box'></div>
The above works great but the problem is I have more than one html element on a page that I want to replace the contents of.上面的效果很好,但问题是我想要替换其中的内容的页面上有多个 html 元素。 So I can't use ids but classes instead.所以我不能使用 id 而是使用类。 I have been told that javascript does not support any type of inbuilt get element by class function.有人告诉我 javascript 不支持任何类型的按类函数的内置 get 元素。 So how can the above code be revised to make it work with classes instead of ids?那么如何修改上面的代码以使其适用于类而不是 id 呢?
PS I don't want to use jQuery for this. PS我不想为此使用jQuery。
解决方案:
参考一: https://stackoom.com/question/FyqO参考二: How to Get Element By Class in JavaScript?