doumi4676 2014-11-23 11:28
浏览 33

Javascript onmousedown toggleDiv(切换2个div?)

So I am really new to javascript, I wouldn't know a line of it if my life depended on it. Somehow I managed to get this to work....

<a href="javascript:void(0)" onmousedown="toggleDiv(' , $message['id'] , ');">

Problem is I need 2 divs to toggle. I thought since they both had the same ID it would toggle them both but it does not. I changed the second one to have "dateline" after the message ID. Not sure how to make this work and would really appreciate a hand :).

I do not wish to use jQuery.

Was asked for this:

    echo '<script type="text/javascript">
            function toggleDiv(divid){
                if(document.getElementById(divid).style.display == \'none\'){
                    document.getElementById(divid).style.display = \'block\';
                    document.pageLoading.TCallLabel(\'/\',\'restart_function\');
                }
                else{
                    document.getElementById(divid).style.display = \'none\';
                }
            }
    </script>';
  • 写回答

1条回答 默认 最新

  • dtnpzghys01643322 2014-11-23 11:44
    关注

    You shouldn't use the same id on two HTML elements. If you need to have a shared selector, you should use a class. Then your script becomes:

    <script type="text/javascript">
            function toggleDiv(divClass){
                var divs = document.getElementsByClassName(divClass);
                for(var i = 0; i < divs.length; i++){
                    if(divs[i].style.display == 'none'){
                        divs[i].style.display = 'block';
                        // Not sure what this is doing, you may want to move it outside of the loop
                        document.pageLoading.TCallLabel('/','restart_function');
                    }else{
                        divs[i].style.display = 'none';
                    }
                }
            }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条