使用jQuery获取多个子体

使用jQuery获取多个子体,jquery,Jquery,我的HTML页面底部出现了一些搜索结果,我想抓取这些结果中出现的每个li元素的一些内容: <ul id="hits"> <li class="hit"> <a href="#" class="image" style="background-image: url('http://...')"></a> <div class="anotherclass"> <h5 class="title">

我的HTML页面底部出现了一些搜索结果,我想抓取这些结果中出现的每个
li
元素的一些内容:

<ul id="hits">

  <li class="hit">
    <a href="#" class="image" style="background-image: url('http://...')"></a>
    <div class="anotherclass">
      <h5 class="title"> Here is a title </h5>
      <p> Here is some paragraph text </p>
    </div>
  </li>

  <li>...</li>

</ul>

我也不确定如何在
style
属性中获取URL,但这可能是另一个SO问题。

您的解决方案已经足够接近了。由于
title
image
是类,因此需要添加
。其次,要获得
h5
,应该使用
text()
而不是
innerHTML

$(“.hit”)。每个(函数(){
让title=$(this.find(“.title”).text();
让img=$(this.find(“.image”).attr(“style”);
console.log(标题)
控制台日志(img)
});

  • 这是一个标题 下面是一些段落文本


您的解决方案非常接近。由于
title
image
是类,因此需要添加
。其次,要获得
h5
,应该使用
text()
而不是
innerHTML

$(“.hit”)。每个(函数(){
让title=$(this.find(“.title”).text();
让img=$(this.find(“.image”).attr(“style”);
console.log(标题)
控制台日志(img)
});

  • 这是一个标题 下面是一些段落文本


首先,你们缺少一个
,应该是
.find(“.title”)
.find(.image”)

其次,要从
h5
获取文本,请使用
.text()
而不是
innerHTML

演示

$(“.hit”)。每个(函数(){
让title=$(this.find(“.title”).text();
console.log(标题)
让img=$(this.find(“.image”).attr(“style”);
});

  • 这是一个标题 下面是一些段落文本


首先,你们缺少一个
,应该是
.find(“.title”)
.find(.image”)

其次,要从
h5
获取文本,请使用
.text()
而不是
innerHTML

演示

$(“.hit”)。每个(函数(){
让title=$(this.find(“.title”).text();
console.log(标题)
让img=$(this.find(“.image”).attr(“style”);
});

  • 这是一个标题 下面是一些段落文本

我想你的意思是利用课堂

我想你的意思是利用课堂

演示:

如果要按类选择元素,则需要添加点
(.)

  • 将find(“title”)更改为find(“.title”)//查找具有title类的元素
  • 将innerHTML更改为text()//获取文本
  • 将find(“image”)更改为find(“.image”)//查找具有image类的元素
  • 将attr(“style”)更改为css(“background image”)//获取属性background image的css值
演示:

如果要按类选择元素,则需要添加点
(.)

  • 将find(“title”)更改为find(“.title”)//查找具有title类的元素
  • 将innerHTML更改为text()//获取文本
  • 将find(“image”)更改为find(“.image”)//查找具有image类的元素
  • 将attr(“style”)更改为css(“background image”)//获取属性background image的css值

您缺少一个
,应该是
。find(.title”)
。find(.image”)
使用
$(this)。find(.image”).css(“background image”)
获取URL谢谢,在我的原始代码中确实缺少一个句点
,您缺少的
,应该是
。find(.title”)
(“.image”)
使用
$(此)。查找(“.image”).css(“背景图像”)
以获取URL谢谢,在我的原始代码中确实有一个句点
缺失。所有发布的答案都很好。我选择此答案是因为它最接近我想要的(
.css(
背景图像
使我更接近提取实际图像URL。所有发布的答案都很好。我选择这一个是因为它最接近我要查找的内容(
。css(
背景图像
使我更接近提取实际图像URL。
$(".hit").each(function() {
  let title = $(this).find("title").innerHTML;
  let img = $(this).find("image").attr("style");
}); 
$(".hit").each(function() {
  let title = $(this).find(".title").text();
  let img = $(this).find(".image").css('background-image');
});