getElementsByClassName问题 [英] getElementsByClassName issue
问题描述
我目前遇到了getElementsByClassName属性的问题。我有一个显示和隐藏功能,当您将鼠标悬停在产品上时会显示一个按钮点击此处我已启用此功能函数使用getElementById属性,但是由于我想要使用类重复此函数的事实是更好的做法。下面的代码是否正确使用ClassName JS函数来解决这个问题?
i'm currently having issue with the getElementsByClassName property. I have a show and hide function which displays a button when you hover over the product click here I have enabled this function using the getElementById property, however due to the fact i want to duplicates of this function using a class is better practice. Is the code below the correct way to address this problem using the ClassName JS function?
另外我一直在阅读IE8中不支持getElementsByClassName这是真的吗?一种解决这个问题的方法吗?
Also i've been reading getElementsByClassName is not supported in IE8 is this true and is there a way to work around this?
<script>
function show(viewProductBtn){
document.getElementByClassName(viewProductBtn).style.visibility = "visible";
}
function hide(viewProductBtn) {
document.getElementByClassName(viewProductBtn).style.visibility = "hidden";
}
</script>
<!--HTML-->
<div class="product-shot-bg" onMouseOver="show('viewProductBtn')" onMouseOut="hide('viewProductBtn')">
<a href="#" class="viewProductBtn"></a>
目前我已将我的开发站点切换回getByID以演示过渡应如何工作。 。
at present i've switched the my dev site back to getByID to demonstrate how the transition should work..
推荐答案
getElement
s
ByClassName
元素<强> s 复数形式。意思是,该函数返回一个数组(实际上是一个NodeList),所以你需要循环遍历数组,或者如果你只想要那个就得到第一个元素。
"Elements" in the plural. Meaning, the function returns an array (actually a NodeList), so you need to either loop through the array, or get the first element if you only want that one.
另请参阅 document.querySelector(。+ viewProductBtn)
一个元素, document.querySelectorAll(。+ viewProductBtn)
为多个,或考虑使用ID和 getElementById
。
See also document.querySelector("."+viewProductBtn)
for one element, document.querySelectorAll("."+viewProductBtn)
for multiple, or consider using IDs and getElementById
.
这篇关于getElementsByClassName问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!