简体   繁体   English

为什么show()仅能瞬间使用?

[英]Why does show() only work for a split second?

I am trying to have the list items appear on click. 我试图让列表项在单击时出现。 When I use the hide() method they start off hidden, but when I add the show() method, it only works for about aa millisecond. 当我使用hide()方法时,它们开始是隐藏的,但是当我添加show()方法时,它只能工作大约一毫秒。 Not sure if it is because it always defaults back to the original setting? 不确定是否因为它总是默认返回到原始设置? I basically just want to click the main list <a> in this case Main List and have the other two appear 我基本上只想单击主列表<a>在这种情况下是“主列表”,然后出现其他两个

 $(function() { $('.sublist').hide(); $(".mainlist").on('click', function() { $('.sublist').show(); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <ul> <li> <a href="" class="mainlist">Main List</a> <li> <a href="" class="sublist">Sublist One</a> </li> <li> <a href="" class="sublist">Sublist Two</a> </li> </li> </ul> 

When you click the link the Whole page refreshes. 单击链接后,整个页面都会刷新。 This Means that your code runs Again: 这意味着您的代码将再次运行:

$('.sublist').hide();

What you should do is prevent default behavior like this: 您应该做的是防止出现以下默认行为:

$(".mainlist").on('click', function(event) {
      $('.sublist').show();
      event.preventDefault();
    });

The problem is that your page is getting reloaded when you click those a elements, because of the href="" on them. 问题是,当你点击你的那些页面重新加载得到a因要素, href=""他们。 So the divs start out hidden, then clicking shows them, but then the page refreshes and they're back to being hidden again (since that's their default state on a freshly-loaded page, thanks to the hide call in your page-ready callback). 因此div开始是隐藏的,然后单击以显示它们,但是页面刷新后又恢复为隐藏状态(由于这是新加载页面上的默认状态,这要归功于页面就绪回调中的hide调用) )。

If you don't want those links to be followed, prevent the default action when they're clicked via preventDefault (or return false ) in a jQuery handler: 如果您不希望遵循这些链接,请在jQuery处理程序中通过preventDefault (或return false )单击它们时阻止默认操作:

$(function() {
  $('.sublist').hide();
    $(".mainlist").on('click', function(e) {
      e.preventDefault();
      $('.sublist').show(); 
    }); 
})

Live Example: 现场示例:

 $(function() { $('.sublist').hide(); $(".mainlist").on('click', function(e) { e.preventDefault(); $('.sublist').show(); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <ul> <li> <a href="" class="mainlist">Main List</a> <li> <a href="" class="sublist">Sublist One</a> </li> <li> <a href="" class="sublist">Sublist Two</a> </li> </li> </ul> 

Alternately, there's a hack you can use: href="javascript:;" 或者,您可以使用一种hack: href="javascript:;" , but it's preferable to make the links meaningful, and then use script to provide that meaning. ,但最好使链接有意义,然后使用脚本提供含义。


Separately: You can't have an li directly inside another li like that, you need another ul (or ol ) in there. 另外:你不能有一个li直接在另一内li一样,你需要另一个ul (或ol在那里)。 See those links for details. 有关详细信息,请参见那些链接。

There are two issues: 有两个问题:

First, you have li elements nested directly inside li elements - if you want a "sub-list", you need to wrap the sub-items inside a ul (see modified code below). 首先,您将li元素直接嵌套在li元素内-如果要“子列表”,则需要将子项目包装在ul (请参见下面的修改代码)。

Second, the link is being followed, which is causing the page to refresh. 其次,正在跟踪链接,这将导致页面刷新。 If ALL you want to have happen is the sublist items to show, then you should use event.preventDefault on the event (see modified code below). 如果要显示的全部是要显示的sublist项,则应在事件上使用event.preventDefault (请参阅下面的修改后的代码)。

 // no-conflict safe document ready jQuery(function($) { $('.sublist').hide(); // click event accepts one argument for the "event" $(".mainlist").on('click', function(e) { // prevent the default "click" action (which is to load the url the link points to) e.preventDefault(); $('.sublist').show(); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <a href="" class="mainlist">Main List</a> <!-- nested li elements need to be wrapped inside a ul --> <ul> <li> <a href="" class="sublist">Sublist One</a> </li> <li> <a href="" class="sublist">Sublist Two</a> </li> </ul> </li> </ul> 

Since you are using anchor a tag, and there is an empty href attribute, the default behavior should be to redirect the page on anchor click. 由于您使用的锚a标签,并且有一个空的href属性,默认的行为应的页面重定向锚点击。 The fix to this is to either use href="javascript:void(0)" or event.preventDefault() within the click callback 解决方法是在点击回调中使用href="javascript:void(0)"event.preventDefault()

 $(function() { $('.sublist').hide() $(".mainlist").on('click', function(e) { /* here, prevents redirection */ if (e) { e.preventDefault() } $('.sublist').show() }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <a href="javascript:void(0)" class="mainlist">Main List</a> <li> <a href="" class="sublist">Sublist One</a> </li> <li> <a href="" class="sublist">Sublist Two</a> </li> </li> </ul> 

This part of your code : 您的代码的这一部分:

$(".mainlist").on('click', function() {
  $('.sublist').show(); 
});

Is telling to show the list only during the click event. 告诉仅在单击事件期间显示列表。

Why not using the fadeIn() effects function instead ? 为什么不改用fadeIn()效果函数呢?

  $( "#appears" ).hide(); $( "#clickme" ).click(function() { $( "#appears" ).fadeIn( "slow", function() { }); }); 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <div id="clickme"> Click here </div> <div id="appears"> <ul> <li>I'm a list</li> </ul> </div> </body> </html> 

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

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