Javascript 按id列出的多个div的下拉div

Javascript 按id列出的多个div的下拉div,javascript,php,html,Javascript,Php,Html,在不同id的多个div中使用下拉列表的最佳实践是什么 我有一个用户流,当我添加一个下拉菜单来添加删除、编辑帖子等时,在顶部帖子上只打开一个div,所以我知道它必须由一个唯一的id完成 也许是一些javascript?这就是我目前所拥有的 <div class="postdrop" id="postdrop'.$streamitem_data['streamitem_id'].'"> <a class="postdrop_button postdrop_close" hr

在不同id的多个div中使用下拉列表的最佳实践是什么

我有一个用户流,当我添加一个下拉菜单来添加删除、编辑帖子等时,在顶部帖子上只打开一个div,所以我知道它必须由一个唯一的id完成

也许是一些
javascript?
这就是我目前所拥有的

<div class="postdrop" id="postdrop'.$streamitem_data['streamitem_id'].'">
    <a class="postdrop_button postdrop_close" href="#">
         <span class="a rrow"></span>
    </a>
    <a class="postdrop_button postdrop_open"href="#postdrop">
         <span class="arrow"></span>
    </a>
    <ul class="postdrop_content">
        <li><a href="members.php">Edit</a></li>
        <li><a href="personalinformation.php">Privacy - </a></li>                                
        <li><a href="editinformation.php">Delete</a></li>                                        
    </ul>
</div>


您可以使用纯css来实现这一点

.postdrop_content {
    display:none;
}
然后可以调用悬停事件上的显示

.postdrop_button.postdrop_open:hover + postdrop_content {
    display:block;
} 
但是,请注意,在这个特定场景中,由于hover事件将显示下拉列表,因此您将无法将鼠标悬停在它上面并保持元素处于打开状态

有两种方法可以解决此问题:

将:hover事件移动到父容器(本例中为postdrop),并在postdrop具有下拉内容时为其提供一个类,例如,
postdrop\u has\u dropdown
。然后,您可以使CSS选择器以该元素为目标,当您将鼠标悬停在下拉列表上时,悬停事件将保持激活状态

.postdrop_has_dropdown:hover .postdrop_content {
    display:block;
}
使用一些简单的jQuery:

$('.postdrop_button.postdrop_open').on('click', function(){
    $(this).next().show();
});

$('.postdrop_button.postdrop_close').on('click', function(){
    $(this).next().hide();
});

您可以使用javascript处理它。我建议使用knockoutjs

使用可观察数组填充提要,您可以使用希望使用的任何事件绑定来执行javascript

<div class="myFeed" data-bind="foreach: myFeed">
    <div class="feedItem" data-bind="event:{click: function(e){$root.doStuff($data e);}}">
        <div class="itemAction" data-bind="event:{mouseOver: function(e){$root.handleMouseOver($data)}}"></div>
    </div>
</div>

澄清一下,你有一个内容提要。你想在点击该提要中的项目时打开一个下拉列表吗?就像用户通过
$streamitem\u data['streamitem\u id']
发布的帖子一样,这是正确的@Mardok。我以前在网上搜索过。但似乎以前没有人问过这样的问题。所以每个帖子都有自己的下拉列表可以打开。谢谢你的工作就像一个绝对的魅力。也很简单,这就是我想要的。
var myController = function()
{
    var self = this;
    this.myFeed = ko.observableArray([]);

    this.handleMouseOver = function(data)
    {
        // Do stuff with data
    };   

    this.doStuff = function(data, e)
    {
        // e is the event that triggered it. e.target is the element you want to manipulate
        // do stuff with data
    };
};