【问题标题】:Select inner element with no id by JQuery通过JQuery选择没有id的内部元素
【发布时间】:2018-10-22 07:06:46
【问题描述】:

假设我有这个div 块:

<div id="some_id" style="display: none;">
    <form action="someAction" method="post">
        <input type="hidden" name="some-name" value="some-value">
    </form>
</div>

我需要在这个div 中选择form。我可以通过$('#some_id') 选择div,但是当我尝试通过$('#some_id').find('form')(或get$('#some_id:form'))选择form 时,我收到此错误:

Uncaught TypeError: $(...).find is not a function

任何可能的解决方案?

【问题讨论】:

  • $ 可能不是 jQuery? console.log($.fn.jquery) 看看有没有版本号。
  • 我得到了Cannot read property 'jquery' of undefined,但$('#some_id') 怎么工作?
  • 评论是“可能重复”而不是问题本身。
  • 副本可以解释为什么 $ 在您的用例中不是 jQuery。 $('#some_id').find('form') 是一个有效的操作。如果 '$' 不是 jQuery,那么这个问题的答案就像“在你的页面中包含 jQuery”一样简单

标签: javascript jquery html dom jquery-selectors


【解决方案1】:

只要 jQuery 正确包含在您的页面中,并且您正确地使用 $jQuery 引用它(如果它处于 noConflict 模式),您的尝试没有任何问题。

console.log(
  $('#some_id').find('form').get()
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="some_id" style="display: none;">
    <form action="someAction" method="post">
        <input type="hidden" name="some-name" value="some-value">
    </form>
</div>

【讨论】:

    【解决方案2】:

    您不希望: 选择您想要的孩子&gt;

    您的选择器应如下所示:

    $('#some_id > form')
    

    还要确保你包含了 jQuery,看起来你的代码找不到它。

    【讨论】:

    • &gt; 在技术上是不必要的。
    猜你喜欢
    • 2013-04-07
    • 2014-06-11
    • 1970-01-01
    • 2011-08-14
    • 2019-12-20
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多