【问题标题】:How to get inner div without id with jquery selector如何使用 jquery 选择器获取没有 id 的内部 div
【发布时间】:2018-01-15 22:15:37
【问题描述】:

我想选择不带idclass 的内部div

<div id="gjs">
   <div>
     pick inner div without id or class
   </div>
</div>

在 jQuery 中:

$("Selector=?").html();

【问题讨论】:

  • abc
  • $('div &gt; div') ?
  • 我试过了,但是不行
  • abc
    gjs 是编辑器 ID,但我必须获取内部 div html
  • 我想要这个结果
    abc

标签: javascript jquery angularjs jquery-ui jquery-plugins


【解决方案1】:

试试这个:

$('.gjs').find('div');

阅读此文档: jQuery Find

【讨论】:

  • 返回一个 jQuery 对象。不是 OP 所期望的。
【解决方案2】:

这里有一个解决方案https://jsfiddle.net/eho6ouye/1/

console.log($('#gjs').find('div').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="gjs"> <div>abc</div> </div>

【讨论】:

    【解决方案3】:

    如果你想要元素本身&lt;div&gt;abc&lt;/div&gt;,那么就这样做:$('#gjs &gt; div')[0]

    这适用于任何 jQuery 对象。 $('.someSelector')[0] 会给你对象的元素,和document.getElementById('id') 一样 $('.someSelector') 将为您提供一个用于所述选择器的 jQuery 对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多