javascript setattribute到多个元素 [英] javascript setattribute to multiple element

查看:676
本文介绍了javascript setattribute到多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多div与类 publish_0 ,我想在点击按钮时更改为 publish_1

I have many div with the class publish_0 that I would like to change to publish_1 on click of a button.

现在我用它但它只改变一个项目。

Right now I use this but it only change one item.

如何将setattribute应用于所有具有 publish_0 的项目。

How to I apply the setattribute to all item that have the publish_0.

document.querySelector('.publish_0').setAttribute("class", "publish_1");


推荐答案

你可以在jquery中使用它:

You can use this with jquery:

$(".publish_0").attr("class", "publish_1")

或者,使用getElementsByClassName并循环返回返回的DOM元素。

Alternatively, use getElementsByClassName and loop through the DOM elements returned.

这篇关于javascript setattribute到多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆