简体   繁体   English

在悬停时显示现有div

[英]Show an existing div on hover

I want to enhance my forum software a bit. 我想稍微增强我的论坛软件。 Lets say I have an div with the ID: 123. 假设我有一个ID为123的div。

Now someone replies to this div and quotes it. 现在有人回复这个div并引用它。 I want to show this div if someone hovers over the quote. 如果有人在报价上盘旋,我想展示这个div。

I tried this so far but no success. 到目前为止我试过这个但没有成功。

$(".referal").hover(function() {
    var id_post = $(this).attr('id')
    $('#reply_' + id_post).show();
});

It gets the right ID but somehow only gets executed if I paste it in the console. 它获得了正确的ID,但只有在我将其粘贴到控制台中时才会被执行。 The existing div also won't be displayed a second time near the quote. 现有的div也不会在报价附近第二次显示。

Am I mistaken the show event somehow? 我是否错误地将节目事件弄错了? I tried with .clone before but also no success. 我之前尝试过.clone但也没有成功。

EDIT: Handle post number as a global id: 编辑:处理邮政编号作为全球ID:

var id_post;
$(".referal").hover(function() {
    id_post = $(this).attr('id')
    $('#reply_' + id_post).show();    
});

$(".referal").on("mouseout",function() {
    $('#reply_'+id_post).hide();
});

JSFiddle 的jsfiddle

https://jsfiddle.net/0vjkxz9y/4/ https://jsfiddle.net/0vjkxz9y/4/

This is a working example of what you want to do: 这是您想要做的一个工作示例:

$('#reply_5').hide();
$(".referal").hover(function() {
    var id_post = $(this).attr('id')
    $('#reply_' + id_post).show();
});

$(".referal").on("mouseout",function() {
    $('#reply_5').hide();
});

https://jsfiddle.net/0vjkxz9y/ https://jsfiddle.net/0vjkxz9y/

Why not using toggleClass ? 为什么不使用toggleClass

 $( ".referal" ).on( "mouseenter mouseleave", function( event ) { var id_post = $(this).attr('id'); $('#reply_' + id_post).toggleClass("active"); }); 
 .test { display: none; } .test.active{ display:block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="referal" id="123">This div has ID 123</div> <div class="test" id="reply_123">This div has ID reply_123</div> 

try this code 试试这段代码

$(".referal").hover(function() {
  var id_post = $(this).attr('id')
  $('#reply_'+ id_post +'').show();
});

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

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