Toggle SPAN Class along with this div toggle

若如初见. 提交于 2019-12-07 01:11:28
<script type="text/javascript">
  $("document").ready(function(){
    $("#myspan").click(function(){
      $(this).toggleClass("die2").toggleClass("die3");
      $("#mydiv").toggle();
    });
  });
</script>

That's it

You don't need jQuery, though you might like it. All you need to do is use an element's classList object. You can do a lot of cool things with classList:

el.classList.add("myClassName")      //adds class (does nothing if el already has that class)
el.classList.remove("myClassName")   //removes class (does nothing if el doesn't have that class)
el.classList.toggle("myClassName")   //toggles class 

el.classList.contains("myClassName") //returns true if el has that class, false if not.

Here's a modified version of your code, as an example of what you could do - though I'm not sure it's exactly what you want to do, but it should point you in the right direction.

<div id="mydiv" class="divHide">
    <img src='http://www.test.com/mypopad.png' alt='' />
</div>

<a href="javascript:;" onmousedown="document.getElementById('mydiv').classList.toggle('divHide');document.getElementById('myspan').classList.toggle('die2');document.getElementById('myspan').classList.toggle('die3');">
    <span id="myspan" class="die2"><!-- --></span>
</a>

(I'm toggling a class on the div as well to show/hide it, instead of your if/else checking of the style attribute.)

I sugest jQuery:

mydiv.toggle() or mydiv.removeClass("die2").addClass("die3")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!