最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change section opacity onclick? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to change the opacity of a with javascript when a div is clicked.

Here is my html:

<section id="section2">
<img onclick="one()" class=img src="1.png" />
</section>
<section id="6-1">
    Hello World
</section>

And my javascript:

function one() {
    document.getElementById('6-1').style.opacity = '1';
    document.getElementById('section2').style.opacity = '0';
}

For some reason, its not working

Thanks!

I'm trying to change the opacity of a with javascript when a div is clicked.

Here is my html:

<section id="section2">
<img onclick="one()" class=img src="1.png" />
</section>
<section id="6-1">
    Hello World
</section>

And my javascript:

function one() {
    document.getElementById('6-1').style.opacity = '1';
    document.getElementById('section2').style.opacity = '0';
}

For some reason, its not working

Thanks!

Share Improve this question asked Dec 8, 2012 at 22:32 Thomas LaiThomas Lai 3171 gold badge6 silver badges16 bronze badges 4
  • 1 What browser are you testing in? – Thierry Blais Commented Dec 8, 2012 at 22:33
  • 1 And what does your css look like? – Thierry Blais Commented Dec 8, 2012 at 22:34
  • 3 ID's starting with numbers are invalid, other than that it works just fine -> FIDDLE – adeneo Commented Dec 8, 2012 at 22:46
  • When using opacity, you should try to look for cross-browser patibility. So, IE 9+ and most modern browsers use opacity=1, IE8- uses filter='alpha(opacity=100)', others use -moz-opacity=1 and -khtml-opacity=1. – user1694691 Commented Dec 8, 2012 at 23:38
Add a ment  | 

2 Answers 2

Reset to default 2

Try to set the value without the ' ', like this:

document.getElementById("section2").style.opacity = 1;

It worked for me just fine.

Like ModernDesigner said, enquote your attributes and change your ids to not start with numbers, but other than that, I still couldn't figure out your problem. But for now you can use jQuery to make it work.

Here's a Fiddle that toggles it every time you click it, so hope this helps

It kept saying the function wasn't defined, so maybe you had some incorrect syntax or something. For now though i used jQuery's .click() function instead of onClick= because it wasn't working.

发布评论

评论列表(0)

  1. 暂无评论