【问题标题】:Showing/hiding the next div on mouseover of the previous div在鼠标悬停前一个 div 时显示/隐藏下一个 div
【发布时间】:2011-07-25 05:07:51
【问题描述】:

这是我的 php 代码:

echo '<div class="code" rel="$url">$title</div>
    <div class="tip">Copy and open site</div>';

以上是循环的回显结果。即可能有 0->n 像下面的 html 结构

<div class="code" rel="....">...</div>
        <div class="tip">Copy and open site</div>

现在,假设有 6 个类似上面的结果,我想使用 jQuery 来获取当我将鼠标悬停在第一个 &lt;div class="code" rel="...."&gt;...&lt;/div&gt; 上时,唯一的第一个提示 div 将显示。当我将鼠标移出时,div 将被隐藏。

我认为单独使用 jQuery 无法达到这种效果。代码里肯定加了一些php,不知道怎么弄?

【问题讨论】:

  • 我认为 $('.code').mouseover( function(){this.next().show()}, function(){this.next().hide() }) ;
  • @Arun:在jsfiddle 中测试它并将其作为答案发布:)
  • 如何让默认的tip div处于隐藏状态?

标签: php jquery


【解决方案1】:

您需要更具体地参考提示 DIV,而不仅仅是类选择器。 比如……

  <div id="link1" class="code" rel="....">...</div>
    <div id="tip1" class="tip">Copy and open site</div>

然后让#link1的hover属性指向你的JQuery显示代码。

【讨论】:

  • 不,jQuery 可以找到下一个元素或具有给定类名的下一个元素。
  • 谢谢。我自己有点老派...... pre js-library。
【解决方案2】:
$( 'div.code' ).mouseover( function() {
    $( this ).next( 'div.tip' ).show();
} );

$( 'div.code' ).mouseout( function() {
    $( this ).next( 'div.tip' ).hide();
} );

根据您的第一条评论进行修改:

有几种可能:

$( document ).ready( function() {  // added document ready for completeness
    $( 'div.code' ).mouseover( function() {
        $( this ).next( 'div.tip' ).show();
    } );
    $( 'div.code' ).mouseout( function() {
        $( this ).next( 'div.tip' ).hide();
    } );

    // this is a way after declaring previous stuff:
    $( 'div.code' ).trigger( 'mouseout' );

    // or simply do:
    $( 'div.tip' ).hide();
} );

【讨论】:

  • 如何让默认的tip div处于隐藏状态?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-03
相关资源
最近更新 更多