【问题标题】:Show and Hide elements using jQuery使用 jQuery 显示和隐藏元素
【发布时间】:2013-03-03 11:12:26
【问题描述】:

我目前正在做这个:

<div id='container'>

<div id="box1">
<h1>Share it!</h1>
</div>    

<div class="box" style="visibility: hidden">
<a href="#" class="bt btleft">Facebook Button here</a>
<a href="#" class="bt btright">Twitter Button here</a>
</div>
</div>


$("#container").hover(function () {
   $("#container div").css("visibility","visible");
},
function () {
   $("#container div").css("visibility","hidden");
});

http://jsfiddle.net/L9USt/3/

我想要实现的就像网站Mashable

我想要实现的是,当我将鼠标悬停在“分享它!”这个词上时,它会自动隐藏并显示链接(在同一个确切位置)。我在这里卡了一段时间,有人可以帮忙吗?

【问题讨论】:

  • 您需要在鼠标悬停时隐藏 box1 并在悬停时显示

标签: javascript jquery show-hide


【解决方案1】:

在您的 HTML 中稍作改动,您可能会发现这很有用。只需使用 jQuery 的.hover 功能来切换状态。

HTML

<div class="social">
     <h1>Share this</h1>

    <div class="networks">
        <p>Twitter</p>
        <p>Facebook</p>
    </div>
</div>

CSS

.networks {
    display:none;
}

JS

$(".social").hover(function() {
    $("h1", this).hide();
    $(".networks", this).fadeIn();
}, function() {
    $(".networks", this).hide();
    $("h1", this).fadeIn();
});

fadeIn() 只是添加了一个很好的淡入淡出效果,你也可以在那里使用.show()

JSfiddle.

【讨论】:

  • 哇!这是完全相同的事情!谢谢@MarcoK。谢谢谢谢谢谢!
【解决方案2】:

使用 html 函数在父级中动态加载内容。 示例:http://jsfiddle.net/slown1/TqGFQ/

解决办法如下:

HTML:

<div id='container' style="border:1px solid red;">
    <h1>Share it!</h1>
</div>

JS:

$("#container").hover(function () {
    $("#container").html("<a href='#' "+
     "class='bt btleft'>"+"Facebook Button here</a><a href='#'" +
     "class='bt btright'>Twitter Button here</a>'");
    },
      function () {
          $("#container").html("<h1>Share it!</h1>");
      });

【讨论】:

    【解决方案3】:

    您需要在悬停时隐藏 box1 并在悬停时显示

     $("#container").hover(function () {
            $('#box1').hide();
            $('.box').show();     
       },
      function () {
           $('.box').hide();     
           $('#box1').show();
       });
    

    和你的 html

    <div id="box1">
    <h1>Share it!</h1>
    </div>    
    
    <div class="box" style="display:none">
    <a href="#" class="bt btleft">Facebook Button here</a>
    <a href="#" class="bt btright">Twitter Button here</a>
    </div>
    

    【讨论】:

    • 您好,谢谢您的意见。不幸的是,如果我删除我的光标。缺少“分享”文本。它应该恢复到它。
    【解决方案4】:

    这对你有用吗?我认为它很简单,适合你

    http://jsfiddle.net/mztcn/

    $("#container").hover(function () {
        $(".box").css("visibility", "visible");
    }, function () {
        $(".box").css("visibility", "hidden");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多