【问题标题】:jQuery Selector: show onclickjQuery 选择器:显示 onclick
【发布时间】:2019-07-07 22:31:12
【问题描述】:

我在让某个 div 显示 onclick 时遇到了一些麻烦。有接盘侠吗?

HTML

<div style="float:left;width:40px;">
<span class="productControl" style="position:relative;">
    <div class="productMenuHolder" style="display:none;">
    <ul class="productMenuList">
        <li class="productMenuItem">Add to Collection</li>
        <li class="productMenuItem">Share</li>
    </ul>
    </div>
</span>
</div>

jQuery

$("span.productControl").click(function(){
    $(this).next().show();
});

productMenuHolder 似乎没有出现!

【问题讨论】:

  • 您是否使用$(function () { ... }); 来确保您的处理程序被绑定?另外,为什么您的跨度包含您的 div?这会在以后引起问题。

标签: jquery html jquery-selectors


【解决方案1】:
$(this).find('.productMenuHolder').show()

我建议您仔细查看标记。

【讨论】:

  • 谢谢@meder。为什么 .next 不起作用? (我会接受你的回答,但堆栈会让我在 9 分钟内完成)
  • next 表示下一个兄弟姐妹,而不是孩子。 span 拥有 div。
  • 啊...愚蠢愚蠢的愚蠢 :: 用手敲头:: 作为后续,当有人点击除 productMenuHolder div 之外的任何地方时,我如何让弹出 div 隐藏?
猜你喜欢
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多