如何选择具有相同类名的所有元素?
我有一个布尔变量。它存储在隐藏的输入字段中。基本上,如果用户已登录,则 false
,如果不是,则 true
。
I have a Boolean variable. It is stored in a hidden input field. Basically, if the user is signed in, it is false
, if not, it is true
.
有下载按钮可以链接到文件下载。我的目的是使它,如果他们没有登录,按钮将不会显示,并且链接将无法工作(有一个警报说他们需要登录或其他东西会很好,但那会可能比它的价值更多的努力。)
There are download buttons which will link to a file download. My aim is to make it so that, if they aren't signed in, the button will not show, and the link will not work (it would be nice to have an alert saying they need to sign in or something, but that would probably be more effort than it's worth).
我有一个执行 onload
的函数:
function hide_download_btns(){
if (document.getElementById('download_btn_var_input').value == "true") {
document.getElementsByClassName('project_download_btn').item(0).hidden = true
}
}
我的问题是它要求第n个术语 .item(0)
。这是它选择执行该函数的div的地方,但是,我希望该函数能够影响所有 div
s,并使用类名' project_download_btn'。
My problem is where it asks for the nth term .item(0)
. This is where it selects the div on which to perform the function, however, I want the function to affect all div
s with the class name 'project_download_btn'.
我不是jQuery的粉丝,所以尽可能避免这种情况。
I'm not a fan of jQuery, so it would be great to avoid that if possible.
你可以简单地遍历元素,而不是仅仅取第0个。
You can simply loop through the elements instead of just taking the 0th.
var buttons = document.getElementsByClassName('project_download_btn');
for(var i=0; i< buttons.length; i++){
buttons[i].hidden = true;
}