我有這樣的代碼。現在,我想與條件替換類名「內容」舊值:如果該值在類在排列價值的一致下面,然後追加到它喜歡:(例)用條件追加到同一個類?
<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中的某個值,則只需進行替換。 –