【问题标题】:Change Text and Icon using jQuery使用 jQuery 更改文本和图标
【发布时间】:2015-08-18 08:16:14
【问题描述】:

当您单击“隐藏信息”时,我想将文本从“隐藏信息”更改为“显示信息”并更改图标:https://cdn4.iconfinder.com/data/icons/seo-and-data/500/view-content-window-16.png

当再次点击文本时,它应该从“显示信息”变为“隐藏信息”。

请记住,当您显示信息时,应显示文本“teststests”。如果您想隐藏“teststests”,请单击“隐藏信息”。

我不知道该怎么做。

谢谢!

$(document).ready(function(){
    $("div#test").click(function(){
        $("#test2").toggle();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="test2" style="display:none">
teststests
</div>

<div id="test">
<img src="https://cdn0.iconfinder.com/data/icons/octicons/1024/eye-16.png" /> Show Information
</div>

【问题讨论】:

  • 查看我的帖子,我已经包含了可以防止 div 在淡入和淡出时重叠的 CSS。这也适用于 Amit 的回答中的“.fadeToggle()”。我无法将 CSS 添加到我的小提琴中,因为我刚刚发布了此评论并在我的手机上进行了编辑,并且由于某种原因,它不允许我将 CSS 字段定位在那里啊哈。您可以将其复制并粘贴进去,它会正常工作。希望这会有所帮助。

标签: jquery


【解决方案1】:

检查这个fiddle

代码

$(document).ready(function(){
    $("div#test,div#test2").click(function(){        
        $("div#test,div#test2").toggle();
    });
});

HTML

<div id="test">
<img src="https://cdn0.iconfinder.com/data/icons/octicons/1024/eye-16.png" /> Show Information
</div>

<div id="test2" style="display:none">
    <div>teststests</div>
<img src="https://cdn4.iconfinder.com/data/icons/seo-and-data/500/view-content-window-16.png"/>
    hide information
</div>

【讨论】:

  • 文本“teststests”在哪里?
  • @HelloWorld 您希望始终显示它吗?如果是,让我更新答案
  • 这是一个比我更好的答案。投票。
【解决方案2】:

这是假设你为你的图标使用了很棒的字体,为你的 javascript 使用了 jquery

你的标记

<div>
    <a href="#" class="toggle show"><i class="fa fa-hide"></i> Show Information</a>
</div>

你的 jquery

$(function () {
    $('.toggle').click(function() {
        if ( $(this).hasClass('show') ) {
            //replace the text
            $(this).text('Hide information');
            //replace the icon
            $(this).find('i').removeClass('fa-hide').addClass('fa-show');
            $(this).removeClass('show').addClass('hide);
        } else {
            //replace the text
            $(this).text('Show information');
            //replace the icon
            $(this).find('i').removeClass('fa-show').addClass('fa-hide');
            $(this).removeClass('hide').addClass('show);                
        }
    });
});

应该这样做 干杯。

【讨论】:

    【解决方案3】:

    正是你想要的:

    js部分:

    $(document).ready(function(){
        $("div#test").click(function(){
    
         if ($('#test2').css('display') == 'none') {
          $("#test2").show();
            $("#changeme").html("Hide Information");
             $("#changeimg").attr("src","https://cdn4.iconfinder.com/data/icons/seo-and-data/500/view-content-window-16.png");
    }     
            else{
                $("#test2").hide();
            $("#changeme").html("Show Information");
                $("#changeimg").attr("src","https://cdn0.iconfinder.com/data/icons/octicons/1024/eye-16.png");
    
            }   
     });
    });
    

    您只需要在 html 中进行一些更改,例如添加一个 ID:

    <div id="test2" style="display:none">
    teststests
    </div>
    
    
    
    <div id="test">
    <img id="changeimg" src="https://cdn0.iconfinder.com/data/icons/octicons/1024/eye-16.png" /> 
    <span id="changeme">Show Information</span>
    </div>
    

    查看http://jsfiddle.net/2pys32qj/

    【讨论】:

      【解决方案4】:

      FIDDLE

      HTML:

      <div id="show" style="opacity: 1;">Click this to show (insert wanted icon)</div>
      
      <div id="hide" style="opacity: 0;">Click this to hide (insert another icon)</div>
      
      <p id="text" style="opacity: 0;">testest</p>
      

      jQuery:

      $(document).ready(function(){
      $('#show').click(function(){
      $('#show').fadeOut(500, 0);
      $('#text').fadeTo(500, 1);
      $('#hide').fadeTo(500, 1);
      });
      $('#hide').click(function(){
      $('#show').fadeTo(500, 1);
      $('#text').fadeOut(500, 0);
      $('#hide').fadeTo(500, 0);
      });
      });
      

      CSS:

      #hide, #show{
      position: absolute;
      top: 0;
      }
      

      【讨论】:

      • 什么文字? “显示信息”和“隐藏信息”?
      • 检查新小提琴。
      • HelloWorld想要的是当你点击“显示信息”时,文档会显示“teststests”,如果你点击“隐藏信息”,“teststests”会被隐藏。
      • 是的,我刚刚修好了。它不在最初的问题中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多