简体   繁体   English

我可以使切换链接在点击时消失吗?

[英]Can I make the toggle link disappear on click?

I've applied the toggle function so that when a user clicks on, say, "Show More", a bunch of further HTML shows up. 我已经应用了切换功能,以便当用户单击“显示更多”时,将显示更多的HTML。 For now a re-click makes the revealed HTML disappear. 现在,再次单击可使显示的HTML消失。 I know I can change it so that the toggle text changes to "Show Less", but that's not what I want. 我知道我可以更改它,以便将切换文本更改为“显示较少”,但这不是我想要的。 What I want is for the "Show More" toggle to completely disappear, and ideally for the revealed HTML to appear not below where the toggle text was, but over top of it, in its place. 我想要的是“显示更多”开关完全消失,并且理想情况下,所显示的HTML不在开关文本的下方显示,而是在其上方显示。 Is this possible? 这可能吗? I've done a fair bit of searching but can't find an answer. 我已经做了相当多的搜索,但是找不到答案。

I've got the basics so far: 到目前为止,我已经掌握了基本知识:

<a href="javascript:toggletext('mytext')"><p>Show More</p></a>
<div id="mytext" style="display: none;">
blah blah text and images blah blah
</div>

and

<script>
    function toggletext(cid) {
        if (document.getElementById(cid).style.display == "none") {
            document.getElementById(cid).style.display = "block";
        } else {
            document.getElementById(cid).style.display = "none";
        };
    }
</script>

As a side note, the revealed HTML has images in it. 附带说明一下,显示的HTML中包含图像。 I'd prefer them not to preload. 我希望他们不要预先加载。 Is that a default, or would I have to specify this? 这是默认值吗,还是我必须指定它?

Thanks in advance for any help. 在此先感谢您的帮助。 (And my apologies if this answer exists elsewhere.) (如果这个答案在其他地方存在,我深表歉意。)

There are many ways you can do this. 您可以通过多种方式执行此操作。

You are missing few things so I would recommend read more. 您缺少一些东西,所以我建议您阅读更多。 href is hyperlink reference you can attach events in the tag. href是超链接引用,您可以在标签中附加事件。 with a tag use preventDefault to avoid any default behaviour. a标签使用preventDefault以避免任何默认行为。

You can even look for animate API. 您甚至可以寻找animate API。 there is Jquery show / less plugin as well. 还有Jquery show / less插件。

Hope this demo gives you direction and one of the way tou can resolve it. 希望本演示为您提供指导,并且您可以通过一种方式解决它。 :)

What the code is doing below 代码在下面做什么

  • On click event of a tag it checks for is(":visible") and adjust the show and hide accordingly. click的事件a标签它会检查is(":visible")并调整节目,并相应地隐藏。

One of many code sample: 许多代码示例之一:

  $("#hulk").click(function () {

      if ($("#mytext").is(":visible")) $("#mytext").hide()
      else $("#mytext").show();
  });

HTML 的HTML

<a id="hulk"><p>Show More</p></a>

<div id="mytext" style="display: none;">blah blah text and images blah blah</div>

your html should look like this: 您的html应该看起来像这样:

<div id="data_to_show">
  <button id="show_data">Show More+</button>
  <p id="data_p" style="display:none;">
    some data to show here... 
  </p>
</div>

and the javascript: 和javascript:

$(function(){
  $('#show_data').click(function(){
    $('#show_data').hide();
    $('#data_p').show();
  });
});

please note, in this example there is no way to "show less" because you wanted the button to disappear. 请注意,在此示例中,无法“显示较少”,因为您希望按钮消失。

If you want "show more" to appear below, you can simply move your <a> tag below <div> tag. 如果要在下方显示“显示更多”,只需将<a>标记移动到<div>标记下方。

For the toggle you should use jQuery as it will make your life easier. 对于切换,您应该使用jQuery,因为它将使您的生活更轻松。

HTML 的HTML

<div id="mytext" style="display: none;">
    blah blah text and images blah blah
</div>
<a id="trigger" href="javascript:void(0);"><p>Show More</p></a>

JAVASCRIPT JAVASCRIPT

$('#trigger').bind('click', function(){
    $(this).hide();
    $('#mytext').show();
});

As for the images, it's a matter of browser. 至于图像,则取决于浏览器。 Some modern browsers do not preload images when display is set to none. 当显示设置为无时,某些现代浏览器不会预加载图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM