我有这样的代码。现在,我想与条件替换类名“内容”旧值:如果该值在类在排列价值的一致下面,然后追加到它喜欢:(例)用条件追加到同一个类?
<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代码是阻止的,只有类是允许的。帮我解决这个问题。由于
查找范围的DOM元素的内容是什么?具体问题是什么? –
@DaveNewton:如果该类具有Array中的某个值,则只需进行替换。 –