我有这样的代码。现在,我想用条件替换类名“content”中的旧值:如果类中的值与下面的 Array 中的值之一一致,则像这样附加到它:(参见示例)
<div class="content">style1</div>
<div class="content">Stackover</div>
<div class="content">style3</div>
<div class="content">somthing</div>
<div class="content">style1</div>
<script>
var styletodisplay = ["style1", "style2", "style3"];
for(m = 0; m < styletodisplay.length; m++)
{
//if the value get from class "content" coincides with one of the value in Array then append to that class(not for all) with HTML : <p>styleN is added</p>
//For example : if the value get from class "content" is style3, then you need to append to that class a HTML like : <p>style3 is added</p>
}
</script>
这意味着,我们有结果:
<div class="content"><p>style1 is added</p></div>
<div class="content">Stackover</div>
<div class="content"><p>style3 is added</p></div>
<div class="content">somthing</div>
<div class="content"><p>style1 is added</p></div>
我怎样才能做到这一点?我知道我们应该使用“ID”来表示唯一但 HTML 代码是块,只允许使用类。帮我解决这个问题。谢谢