【问题标题】:show hide html div using javascript使用javascript显示隐藏html div
【发布时间】:2015-05-27 04:19:27
【问题描述】:

我想使用 javascript 自定义这个 show hide html div。

HTML

<a href="#" class="clickme">Menu 1</a>
<div class="box">
   <span><span class="labelText"><span></span>Number 1</span></span>
    <input type="text" value="" maxlength="50"><br/>
    <span><span class="labelText"><span></span>Number 2</span></span>
    <input type="text" class="inputclmsize1"  value="" maxlength="50"  aria-invalid="false"><br>
    <a href="">Submit</a>                                                                                     
</div>

<a href="#" class="clickme">Menu 2</a>
<div class="box">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,
</div>

CSS 代码

body {
font: 12px/16px sans-serif;
margin: 10px;
padding: 0;}

.clickme {
background-color: #eee;
border-radius: 4px;
color: #666;
display: block;
margin-bottom: 5px;
padding: 5px 10px;
text-decoration: none;}

.clickme:hover {
text-decoration: underline;}

.box {
background-color: #ccc;
border-radius: 4px;
color: #333;
margin: 5px 0;
padding: 5px 10px;
width: auto;}

Javascript 代码

$('.box').hide();
// Make sure all the elements with a class of "clickme" are visible and bound
// with a click event to toggle the "box" state
$('.clickme').each(function() {
    $(this).show(0).on('click', function(e) {
        // This is only needed if your using an anchor to target the "box" elements
        e.preventDefault();

        // Find the next "box" element in the DOM
        $(this).next('.box').slideToggle('fast');
    });

});

当我单击 Menu1 行时,它会出现两个带有提交链接的文本列。 我想,当我点击提交按钮时,菜单 1 关闭并且菜单 2 描述自动打开。我怎样才能做到这一点。请问有人可以帮忙吗? :( 小提琴链接在这里 http://jsfiddle.net/rn4qdyue/2/

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    您可以向提交按钮添加点击事件,然后切换每个框。

    $("#submit").click(function(e){
        e.preventDefault();
        $("#box1").slideToggle('fast');
        $("#box2").slideToggle('fast');
    });
    

    请注意,我在提交按钮中添加了 ID,并在 DIVs 中添加了两个框。

    小提琴:http://jsfiddle.net/rn4qdyue/6/

    【讨论】:

      【解决方案2】:

      添加提交点击的点击处理程序。

      <a href="" id="submit">Submit</a>  
      

      $('#submit').click(function(e){
          e.preventDefault();
          var $parent = $(this).closest('.box');
      
          $parent.slideToggle('fast');
          $parent.next('.clickme').click();
      });
      

      Updated Fiddle

      【讨论】:

        【解决方案3】:

        检查以下解决方案。

        $('#submit').on('click', function(e) {
            e.preventDefault();
            var $el = $(this).parent('.box');
        
            $el.slideUp('fast');
            $el.next().next('.box').slideDown('fast')
        });
        

        Updated Fiddle

        【讨论】:

        • 这个解决方案值得获得奖牌。如果单击提交按钮时菜单 2 已打开,则菜单将保持打开状态。
        【解决方案4】:

        您可以将id 添加到您的提交中。

        <a id="submit" href="">Submit</a>  
        

        在 JS 中这样做:

        $('#submit').click(function(e){
            e.preventDefault();
          $('.box').slideToggle('fast');
        })
        

        Fiddle.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多