简体   繁体   English

单击链接后如何立即显示隐藏的div

[英]How to show hidden div immediately after clicking on link

So I've made a function that, when you click on a button, a certain div comes up with its own content. 因此,我创建了一个函数,当您单击按钮时,某个div会显示其自己的内容。 When clicking on the button again, the div will hide and another div will show up. 再次单击该按钮时,该div将隐藏,并显示另一个div This div will always show up when none of the other three divs aren't selected. 当未选择其他三个div时,此div将始终显示。

The problem is when I'm adding an href tag with an anchor link which is connected to each div , that I must click twice on the button before the hidden div will show. 问题是当我添加带有连接到每个div的锚链接的href标签时,必须在按钮上单击两次,然后隐藏的div才会显示。

Check fiddle here: http://jsfiddle.net/449r8Lwv/ 在此处检查小提琴: http : //jsfiddle.net/449r8Lwv/

So as you can see, when you click on one of the buttons, nothing happens except that the url changes which is a good thing. 如您所见,当您单击其中一个按钮时,除了url更改之外,什么都没有发生,这是一件好事。 But clicking AGAIN on the same button lets you show the hidden div. 但是单击同一按钮上的AGAIN可以显示隐藏的div。 This is not what I want, I want the div show up the first time you click on the button already. 这不是我想要的,我希望您一次单击该按钮时显示div。

Also, when going directly to the url with an anchor name in it, It will immediately show the div with it's content that is connected to the anchor, that's a good thing. 另外,当直接转到其中带有锚点名称的url时,它将立即显示具有与锚点连接的内容的div,这是一件好事。 But then if you click another button again, it will show the div that must normally show when NONE of the divs aren't selected which is not the case. 但是,如果再次单击另一个按钮,它将显示通常在未选择任何一个div时通常必须显示的div(不是这种情况)。

Example: You go to url website.com/test.html#2. 示例:您转到网址website.com/test.html#2。 Then when you click on button 3, then content of the connected div must come("3") up but instead the div(named #text) will come up when that one should only come up if none of the divs that are connected to the buttons arent showing up. 然后,当您单击按钮3时,则必须显示连接的div的内容(“ 3”),但只有在没有连接到div的情况下才显示该div(名为#text),按钮出现。

HTML: HTML:

<a href="#1">1</a>
<a href="#2">2</a>
<a href="#3">3</a>
<br><br><br>
<div id="clicks"> 
 <a class="click" id="showInfo" data-target=".1"><button>1</button></a>
 <a class="click" id="showDataInput" data-target=".2"><button>2</button></a>
 <a class="click" id="showHistory" data-target=".3"><button>3</button></a>
</div>
    <div class="1 target" style="display: none;"><a name="1">1</a></div>
    <div class="2 target" style="display: none;"><a name="1">2</a></div>
    <div class="3 target" style="display: none;"><a name="1">3</a></div>
<div id="text">"I WANT THIS DIV GONE EVERYTIME I LET DIV 1, 2 OR 3 SHOW BY CLICKING THE BUTTONS. BUT SHOW UP AGAIN WHEN 1, 2 OR 3 IS NOT SHOWING/SELECTED"</div>

JavaScript/jQuery 的JavaScript / jQuery的

<script>
$(document).ready(function () {
    var $targets = $('.target');
    $('#clicks .click').click(function () {
        var $target = $($(this).data('target')).toggle();
        $targets.not($target).hide();
        $('#text').css('display', $('div.target:visible').length ? 'none':'')
        /*$('#contact_info').toggle(!$targets.is(':visible'));*/
    });
});  
</script>

<script>
  function doToggle(num) {
      var target = $('div.target' + num);
      target.toggle();
      $('.target').not(target).hide();
      $('#text').css('display', $('div.target:visible').length ? 'none' : '')
  }
  $('#clicks .click').click(function () {
      var num = $(this).data('target');
      doToggle(num);
  });
  function handleHash() {
      doToggle("." + location.hash.substring(1));
  }
  window.onhashchange = handleHash;
  $(handleHash);
</script>

Thank you a lot. 非常感谢。

ps: in the fiddle I only put the second script part because of some issues when I put the other part aswell. ps:在小提琴中,由于某些问题,我只放置了第二个脚本部分,而我也放置了另一部分。 If you test it local you should use the whole JavaScript/jQuery part. 如果在本地测试,则应使用整个JavaScript / jQuery部分。

Since the URL is changing, you need to put doToggle(..) in the main section of your code. 由于URL正在更改,因此需要在代码的主要部分中放入doToggle(..)。

Another problem is the data-target. 另一个问题是数据目标。 jQuery may evaluate the number as a number. jQuery可以将数字评估为数字。 So .1 will become 0.1. 因此,.1将变为0.1。 We can add the . 我们可以添加。 in JS. 在JS中。

<div id="clicks">
 <a href="#1" class="click" id="showInfo" data-target="1"><button>1</button></a>
 <a href="#2" class="click" id="showDataInput" data-target="2"><button>2</button></a>
 <a href="#3" class="click" id="showHistory" data-target="3"><button>3</button></a>
</div>
    <div class="1 target" style="display: none;"><a name="1">1</a></div>
    <div class="2 target" style="display: none;"><a name="1">2</a></div>
    <div class="3 target" style="display: none;"><a name="1">3</a></div>
<div id="text">"I WANT THIS DIV GONE EVERYTIME I LET DIV 1, 2 OR 3 SHOW BY CLICKING THE BUTTONS. BUT SHOW UP AGAIN WHEN 1, 2 OR 3 IS NOT SHOWING/SELECTED"</div>

and the JavaScript: 和JavaScript:

  function doToggle(num) {
      var target = $('div.target' + num);
      target.toggle();
      $('.target').not(target).hide();
      $('#text').css('display', $('div.target:visible').length ? 'none' : '')
  }

  $('#clicks .click').click(function () {
      var num = '.' + $(this).data('target');
      if (num === '.' + location.hash.substring(1)) {
          doToggle(num);
      }
  });

  function handleHash() {
      doToggle("." + location.hash.substring(1));
  }

  if (location.hash.substring(1).length > 0) {
      doToggle('.' + location.hash.substring(1));
  }

  window.onhashchange = handleHash;
  $(handleHash);

Edited: In you script before, doToggle was working, but after it executed, the url would change, making it look like doToggle wasn't working. 编辑:在您之前的脚本中,doToggle在工作,但执行后,URL会更改,使其看起来像doToggle在工作。 The click handler should only perform the toggle if the hash url is the same as the toggled div. 仅当哈希网址与切换后的div相同时,点击处理程序才应执行切换。

http://jsfiddle.net/449r8Lwv/12/ http://jsfiddle.net/449r8Lwv/12/

I'd suggest to remove dots in your data-target attribute. 我建议您删除数据目标属性中的点。

So, your HTML will look like this: 因此,您的HTML将如下所示:

<a href="#1">1</a>
<a href="#2">2</a>
<a href="#3">3</a>
<br><br><br>
<div id="clicks"> 
 <a class="click" id="showInfo" data-target="1"><button>1</button></a>
 <a class="click" id="showDataInput" data-target="2"><button>2</button></a>
 <a class="click" id="showHistory" data-target="3"><button>3</button></a>
</div>
    <div class="1 target" style="display: none;"><a name="1">1</a></div>
    <div class="2 target" style="display: none;"><a name="1">2</a></div>
    <div class="3 target" style="display: none;"><a name="1">3</a></div>
<div id="text">"I WANT THIS DIV GONE EVERYTIME I LET DIV 1, 2 OR 3 SHOW BY CLICKING THE BUTTONS. BUT SHOW UP AGAIN WHEN 1, 2 OR 3 IS NOT SHOWING/SELECTED"</div>

And you can try it here . 您可以在这里尝试。

Change 更改

$('#clicks .click')

To

$('#clicks.click')

This should solve your problem. 这应该可以解决您的问题。

So remove the space in your selector 因此,请删除选择器中的空格

example: jsfiddle 示例: jsfiddle

There is flaw in your code thats why it requires two click to show the div. 您的代码中存在缺陷,这就是为什么需要两次单击才能显示div的原因。
Flaw: when user clicks on a link/button say "1" first time both your click and onhashchange handler is getting fired one after another. 缺陷:当用户单击链接/按钮时,第一次说您的clickonhashchange处理程序将被逐个触发。 ie clickHandler is first display the div 1 which is again gets hidden by your onhashchange handler call. 即clickHandler首先显示div 1,它再次被onhashchange handler调用隐藏。
Next time when you click on same link 'No hashchange will occur' and hence only click handler is getting fired which in turn display the div correctly. 下次当您单击相同的链接时,“不会发生哈希更改”,因此只会触发Click处理程序,从而正确显示div。
Solution: I suggest you should only use click handler because of the nature of your requirement. 解决方案:由于您的要求的性质,我建议您仅应使用单击处理程序。 Or if it doesn't fit your requirement, you can set global variable to track if one of the event is fired and check its value before calling doToggle in your hashchangehandler. 或者,如果它不符合您的要求,则可以设置全局变量以跟踪是否触发了一个事件并在调用hashchangehandler中的doToggle之前检查其值。

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

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