简体   繁体   English

我想展示很多同班的div。 该div位于div内,就像工具提示一样

[英]I have plenty of divs with same class, that I want to show. This div is inside a div, so like a tooltip

I'm a noob with jQuery, so I have some problems. 我是jQuery的新手,所以遇到了一些问题。

I have many divs generated with a foreach (PHP), like this: 我有许多用foreach(PHP)生成的div,如下所示:

And then, I have some tooltip divs that look like this: 然后,我有一些如下所示的工具提示div:

    foreach($posts as $post):
        $id = $post['post_id'];
    ?>
        <div id="stor" style="background-color:red; width:100px;">
        <?php echo $post['user'] . " " . $post['contents'] . "<br>";?></div><br>

        <div id="popup" style="display:none; background-color:black; width:100px; height:100px; color:white;"><?php echo $post['date_posted'] .  " " . $post['user']; ?></div>
        <?php
        endforeach; ?>

So, on my first "stor" div, I want to hover over so the first "popup" div show the content. 因此,在我的第一个“ stor” div上,我想将鼠标悬停在第一个“ popup” div上以显示内容。 I hope I wasn't confusing. 我希望我不要混淆。 I'll explain better if needed! 如果需要,我会解释得更好!

You have to change the id to class , because id s are unique by definition. 您必须将id更改为class ,因为id在定义上是唯一的。

Your jQuery would look something like: 您的jQuery如下所示:

$('.stor:first').on('mouseover', function(e){
  mouseenter: function () {
    $('.popup:first').show();
  },
  mouseleave: function () {
    $('.popup:first').hide();       
});

EDIT: 编辑:

Based upon your final comment, you actually want: 根据您的最终评论,您实际上想要:

$('.stor').on('mouseover', function(e){
  mouseenter: function () {
    $(this).find('.popup').show();
  },
  mouseleave: function () {
    $(this).find('.popup').hide();       
});

This assumes that there is only one popup per stor . 这是假设只有一个popupstor Otherwise, use the :first pseudoelement. 否则,使用:first伪元素。 See the documentation for find . 有关查找,请参见文档。

ID s must be unique, you should use classes instead. ID必须是唯一的,而应该使用类。 You could achieve this result with css only without jquery. 您可以仅使用css而不使用jquery来实现此结果。

 .stor { background-color: red; width: 100px; } .popup { display: none; background-color: black; width: 100px; height: 100px; color: white; } .stor:hover + .popup { display: block; } 
 <div class="stor"> user- contents <br> </div> <div class="popup">date_posted - user</div> 

If you want tooltips, this has already been solved in jQueryUI. 如果需要工具提示,这已在jQueryUI中解决。 http://jqueryui.com/tooltip/ http://jqueryui.com/tooltip/

<?php foreach($posts as $post):
    $id = $post['post_id'];?>

    <div class="stor" style="background-color:red; width:100px;"
    title="<?php echo $post['date_posted'] .  " " . $post['user']; ?>">
    <?php echo $post['user'] . " " . $post['contents'] ;?>
    </div>

<?php endforeach; ?>

<script>
$(document).ready(function(){
    $('.stor').tooltip();
});
</script>

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

相关问题 我想在javascript中创建div,因此我不必坐在那里并制作一百个不同的div - I want to create div's in javascript so I do not have to sit there and make a hundred different divs 当我有多个具有相同类名的div时如何显示/隐藏某些div - How to show/hide certain div when i have multiple div with same class name 在Carousel中,我想显示3格,但当我单击时我要移动1格 - In Carousel i want to show 3 divs but when i click i want to move 1 div 我有DIV及其子DIV,我想使用jQuery选择最后一个DIV。 父级DIVS有ID - I have DIV and its child DIV , i want to select last DIV using jQuery . Parent DIVS have id 我想显示滑块div,直到鼠标位于容器div上,jquery中有许多内部div? - I want to show the slider div until the mouse is over the container div with many inner divs in jquery? 如何使用jQuery影响同一类的8个div的单个div? - How can I affect a single div of 8 divs of a same class with jQuery? 我想用div来代替另一个div - I want to show the div in place of the other div 如何显示/隐藏一个 <div> 当单选按钮被点击时 <div> 有同一个班级? - How can I show/hide one <div> when a radio button is clicked on, when all the <div>s have the same class? 我想对同一个类的多个div求和 - I want to sum multiple div's with the same class 我希望相同 class 的每个 div 来选择他们的孩子 - I want each div of the same class to choose their children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM