简体   繁体   English

显示/隐藏点击

[英]Show/Hide On Click

I have one area of space and two body's of text to show. 我要显示一个空间区域和两个正文。 I have two "hyperlinks" above this area and would like to use those to show/hide the text below. 我在此区域上方有两个“超链接”,并希望使用它们来显示/隐藏下面的文本。 Upon first loading the page, nothing will be showing except for the two links. 第一次加载页面时,除了两个链接之外,什么都不会显示。 When you click one link it shows the body of text. 当您单击一个链接时,它将显示文本的正文。 When you click the other link it will hide the previous body of text and show the new text. 当您单击另一个链接时,它将隐藏上一个文本主体并显示新文本。 There are only two hyperlinks, but I would like for the user to be able to toggle back and forth at their convenience. 只有两个超链接,但是我希望用户能够在他们方便的时候来回切换。 Is this possible? 这可能吗? Previously I was using javascript to unhide the text because they were in two different areas. 以前,我使用javascript取消隐藏文本,因为它们位于两个不同的区域。 I am not too experienced with writing code. 我对编写代码不太有经验。 I have found some other answers on this topic useful but most of them use buttons and on click listeners. 我发现有关此主题的其他一些答案很有用,但大多数答案都使用按钮和单击侦听器。 Is there a way to do this using a hyperlink? 有没有办法使用超链接做到这一点? Code examples are very much appreciated! 代码示例非常感谢!

You could define a class in CSS that says "Don't show the text in here" then use JS from the hyperlink click to switch the class of the element? 您可以在CSS中定义一个说“不在此处显示文本”的类,然后使用超链接单击中的JS来切换元素的类?

so your html will contain: 因此您的html将包含:

<a onclick="showText('text1','text2')" href="javascript:void(0);">Show Text 1</a>
<div id="text1" class="hide"> text1 </div>
<a onclick="showText('text2','text1')" href="javascript:void(0);">Show Text 2</a>
<div id="text2" class="hide"> text2 </div>

Your CSS would contain: 您的CSS将包含:

div.hide { display:none; [your properties]; }
div.show { [your properties]; }

and the your JS would look something like this: 并且您的JS看起来像这样:

function showText(show,hide)
{
    document.getElementById(show).className = "show";
    document.getElementById(hide).className = "hide";
}

Does this help at all? 这有帮助吗?

<a id="myLink" href="javascript:void(0)" onclick="javascript:myLinkButtonClick();"> </a>

in javascript you can do this if you use jQuery : 在javascript中,如果您使用jQuery,则可以执行以下操作:

function myLinkButtonClick()
{
    $("#myDiv").hide();
}

or 要么

function myLinkButtonClick()
{
    $("#myDiv").show();
}

Alternatively you can do .toggle 或者,您可以执行.toggle

function myLinkButtonClick()
{
    $("#myDiv").toggle();
}

Many will agree that using anchor tags to execute Javascript (and do nothing else) is a little on the messy side, since, among other things, it generates a hash tag in the address bar which can confuse users. 许多人会同意,使用锚标记执行Javascript(而不执行其他任何操作)有点麻烦,因为除其他事项外,它在地址栏中生成了一个哈希标记,这可能会使用户感到困惑。 That isn't to say that they don't have their place in JS execution. 这并不是说它们在JS执行中没有地位。

It is very possible to achieve this however. 但是很有可能实现这一目标。 Here is one possible solution: 这是一种可能的解决方案:

<a href="#" onclick="show('div1')">Link1</a>
<a href="#" onclick="show('div2')">Link2</a>

<div id="div1">Text1</div>
<div id="div2" style="display:none;">Text2</div>

<script>

    var currentDiv = document.getElementById("div1");
    function show(divID) {

        var div = document.getElementById(divID);

        currentDiv.style.display = "none";
        div.style.display = "block";

        currentDiv = div;
    }

</script>

The script tag defines a variable and a function: currentDiv , which references the currently displayed div element and a show function, which is used for hiding the currently displayed div and showing a new one. script标签定义了一个变量和一个函数: currentDiv ,它引用当前显示的div元素和一个show函数,该函数用于隐藏当前显示的div并显示一个新的div。

The anchor tags at the top, when clicked, call the show function, replacing the currently shown element with the one the anchor tag specifies. 单击顶部的锚标记后,将调用show函数,用锚标记指定的元素替换当前显示的元素。

In order to get elements to show/hide, the code changes the element's CSS display attribute. 为了使元素显示/隐藏,代码更改了元素的CSS display属性。 A value of block shows the div element, and a value of none hides it. block表示div元素,值none则将其隐藏。 The second div has its display property set to none when the page loads. 页面加载时,第二个div的display属性设置为none Javascript will change this attribute when a link is clicked. 单击链接时,Javascript将更改此属性。

No, you do not need JQuery to do this, but it can help. 不,您不需要JQuery来执行此操作,但是它可以提供帮助。

There's a nice jQuery script that does something along these lines, have a look to see if it's any good for you: 有一个不错的jQuery脚本,可以按照以下步骤进行操作,看一看是否对您有好处:

http://api.jquery.com/slideToggle/ http://api.jquery.com/slideToggle/

This is possible, but a more user friendly way of doing this would be with something like jquery tabs. 这是可能的,但是更方便用户的方法是使用诸如jquery选项卡之类的东西。 It's very easy to do it with jquery UI's tab feature, it's all HTML markup with a script that just runs .tabs(); 使用jquery UI的tab功能很容易做到,所有HTML标记都带有只运行.tabs()的脚本。 as the function on the ID of the tab element. 作为tab元素ID上的函数。

Here is a link: Jquery Tabs 这是一个链接: jQuery选项卡

Tabs would be the best way to do this. 制表符将是执行此操作的最佳方法。 There's plenty of tutorials around for jQuery tabs - here's a fairly basic one which outlines the concepts pretty well, and here's a more advanced one (which goes into using CSS to generate rounded corners on tabs). jQuery选项卡上有很多教程-这是一个相当基本的教程,很好地概述了概念,而这里是更高级的教程(使用CSS在选项卡上生成圆角)。

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

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