javascript - 在 div 中选择图像

标签 javascript jquery html

<div>
    <div id ="container">
       <div>
           <img src="some src>
       </div>
       <p>get this text</p>
       <h>hello</h>
     </div>
</div>

我想要主 div 中的图像总数,并且想要提取图像标签以外的 html 标签。
输出:
否。图片数量 =1
strings= <p>get this text</p><h>hello</h>

请给我一个演示

最佳答案

$("#container img").length - 对于 img标签数

为每个提取字符串,

$("#container").find("p, h").not("div").each(function() {
    console.log($(this).get(0)); //Object
});

注意:您正在使用一些奇怪的 <h> 元素是一个无效的 HTML 标签,我认为它是 <h1>或其他一些标题元素。

关于javascript - 在 div 中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968328/

相关文章:

javascript - 元素什么时候真正添加到 DOM 树中?

javascript - Jquery - 在共享相同类名的其他元素之间选择一个元素(单击时)

javascript - 我为什么要使用 Array.toSource?

javascript - 如何向下滚动到文档或正文高度的 90%?

html - 如何强制位置为 :absolute to resize the parent div? 的元素

html - 使用 CSS 修剪 div 中的 HTML 文本

javascript - 如何覆盖 Material 表中的 MuiPaper-root 样式

javascript - 添加相对元素时会发生什么变化?

javascript - 如何在asp.net中更新gridview文本框值

javascript - 使用 Javascript 获取文本框的值