admin管理员组文章数量:1355093
I have the following script in the head Element:
<script>
function changeOpacity(className) {
var elems = document.getElementsByClassName(className);
var index = 0, length = elems.length;
for ( ; index < length; index++) {
elems[index].style.transition = "opacity 0.3s linear 0s";
elems[index].style.opacity = 0.8;
}
}
</script>
<style>
.red_box {width:100px;height:100px;opacity:1;background:red}
</style>
and the following structure in the body Element:
<div onmouseover="changeOpacity('.red_box')">Click to fade red boxes</div>
<div class="red_box">Box 1</div>
<div class="red_box">Box 2</div>
<div class="red_box">Box 3</div>
I'm trying to achieve the following: when the user clicks on the 'Click to fade red boxes' message, the function should make each div with the class: 'red_box' to turn transparent. I would love some pointers (not Jquery please), Thanks!
I have the following script in the head Element:
<script>
function changeOpacity(className) {
var elems = document.getElementsByClassName(className);
var index = 0, length = elems.length;
for ( ; index < length; index++) {
elems[index].style.transition = "opacity 0.3s linear 0s";
elems[index].style.opacity = 0.8;
}
}
</script>
<style>
.red_box {width:100px;height:100px;opacity:1;background:red}
</style>
and the following structure in the body Element:
<div onmouseover="changeOpacity('.red_box')">Click to fade red boxes</div>
<div class="red_box">Box 1</div>
<div class="red_box">Box 2</div>
<div class="red_box">Box 3</div>
I'm trying to achieve the following: when the user clicks on the 'Click to fade red boxes' message, the function should make each div with the class: 'red_box' to turn transparent. I would love some pointers (not Jquery please), Thanks!
Share Improve this question edited Apr 5, 2016 at 4:54 user2840467 asked Apr 4, 2016 at 21:16 user2840467user2840467 9033 gold badges13 silver badges20 bronze badges 2-
1
The opacity is set to
1
in the CSS, and then your JavaScript code also sets it to1
. Did you mean to set it to0
? – Pointy Commented Apr 4, 2016 at 21:18 - Oops! Thanks Pointy - sorry for confusion. Yes, the function should have the opacity at 0.8 I have edited my question now – user2840467 Commented Apr 5, 2016 at 4:53
2 Answers
Reset to default 3Remove the dot in the class name when calling the function:
onmouseover="changeOpacity('red_box')"
document.getElementsByClassName(className)
takes the class name as a string. If you want to use a CSS selector, you can use document.querySelectorAll('.red_box')
instead.
As pointed out by @pointy in his ment, you also set the opacity to the same as the initial value. Probably want to change that to something between 0 and 0.9, depending on the level of transparency you want.
You had set the opacity to the same in the css and the function, and also you needed to remove the dot in onmouseover="changeOpacity('red_box')"
Change your js function and html to be something like the below and it will work nicely.
function changeOpacity(className) {
var elems = document.getElementsByClassName(className);
console.log(elems)
var index = 0,
length = elems.length;
for (; index < length; index++) {
elems[index].style.transition = "opacity 0.3s linear 0s";
elems[index].style.opacity = 0.5;
}
}
.red_box {
width: 100px;
height: 100px;
opacity: 1;
background: red
}
<div onmouseover="changeOpacity('red_box')">Click to fade red boxes</div>
<div class="red_box">Box 1</div>
<div class="red_box">Box 2</div>
<div class="red_box">Box 3</div>
本文标签: htmlUsing javascript to target classStack Overflow
版权声明:本文标题:html - Using javascript to target class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744000985a2573852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论