【问题标题】:Animating Height of Parent Hides Select Options父级隐藏选择选项的动画高度
【发布时间】:2010-01-19 05:39:27
【问题描述】:

当我专注于一个选择框时,我希望隐藏的工具提示出现。当我单击选择框时,动画开始,但选项列表被隐藏。我如何解决这个问题?

<style>
.showme {display:none;}
li {height:25px;background:red; }
select{z-index:100;}
p{margin:0px;padding:0px;}
</style>
<script type="application/javascript">
$(document).ready(function() {

    $("select")
        .focus(function(){
            var myParent = $(this).parent("li");
            var myInfo = $(this).siblings(".showme");

            $(myParent).data("myoldheight", $(myParent).height());

            $(myInfo).css("display","block");                               
            var totalHeight = $(myParent).height() + $(myInfo).height();

            $(myParent).animate({
                "height": totalHeight + "px"                               
            },3000,function() {console.log("animated");})               
        })

        .blur(function() {

            $($(this).parent("li")).animate({
                "height": $(this).parent("li").data("myoldheight") + "px"                              
            },500) 

            $(this).siblings(".showme").hide();
        })
        })
</script>
<form>
<ul>
<li>
<label for="test">Test</label>
<select id="test" name="test">
    <option value=""></option>
    <option value="a">a</option>
    <option value="b">b</option>
</select>
<p class="showme">This is my text</p>    
</li>
</ul>
</form>

【问题讨论】:

  • 这看起来很奇怪。我只是玩了一下它,似乎无法找出发生了什么。我不确定为什么调用 animate 会隐藏选择选项。这可能是应该提交给 jQuery 团队的东西

标签: jquery select animation height parent


【解决方案1】:

我认为您处理问题/标记错误。尝试对结构进行一点分割,这样您就不会为父级设置动画。例如:

HTML:

<select id="selSomething">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<div class="tooltip">More information!</div>

Javascript:

 $(document).ready(function() {
      $('#selSomething').focus(function() {
          var $tip = $(this).next('.tooltip');
          if($tip.length > 0) {
            $tip.slideDown();
          }
      });
      $('#selSomething').blur(function() {
          var $tip = $(this).next('.tooltip');
          if($tip.length > 0) {
            $tip.slideUp();
          }
      })
  });

记住,简单就是完美:)

【讨论】:

  • 我喜欢您的简单回答,但不幸的是,渲染的标记超出了我的控制范围。 :( 在另一个世界里,这会很完美!
猜你喜欢
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
相关资源
最近更新 更多