我有一個調用的加入到我的div類的隨機數的函數:如何添加類的列表,並只顯示JavaScript中的最後一個?
htmlFields[i].classList.add(numbers[i]);
numbers[i]
可以添加任意類:'one','two','three','four','five'
而我的div已有'static'
,'static2'
類被添加隨機班像這樣:
<div class="static static2 five one">
<div class="static static2 two four one">
<div class="static static2 one three four">
<div class="static static2 five one">
<div class="static static2 one five four">
我想保留靜態類並只添加隨機類中的最後一個類。我試圖使用setAttribute()
方法,但它刪除了靜態類。
那麼我怎樣才能添加最後一個'隨機類',並保持靜態類在同一時間?
請出示所有你試圖添加最後一個隨機類的代碼。 – AtheistP3ace
您可能正在尋找'addClass()'而不是'setAttribute',它改變了所有的類元素 –
@luigonsec - 'addClass'是一個jQuery(jQuery沒有被用在這個問題中)wrapper圍繞'classList.add' (在問題的第一個代碼示例中使用)。 – Quentin