簡體   English   中英

jQuery獲取子DIV的HTML內容

[英]jQuery get HTML contents of child DIV

不知道為什么我不能讓它工作。 現在嘗試了一些排列。

在這里建立了一個JSFIDDLE

我無法讓這個 jQuery 返回目標元素的內部 HTML。 是因為層次結構中的子元素數量多嗎?

jQuery:

modalContentLeft  = 'Project: ' + $('#mob-' + uuid + ' div.project-name').html();

我也嘗試使用.find()沒有成功。 謝謝你的幫助。

我發現了你的錯:你在到達名稱之前關閉了 id 的 div 標簽。 您在<div class="mobile-item-row clearfix">之前關閉它,因此選擇器在其中找不到子項".project-name" ,因為沒有。 如果您刪除結束標記並在 HTML 末尾關閉它並稍微更改選擇器的語法,則它可以工作。

Project: ' + $('#mob-'+uuid+' .project-name').html()

給你: https : //jsfiddle.net/ucupbtsf/5/

首先你在這里的帖子和你的 jsfiddle 反映的不一樣......

JsFiddle :

modalContentLeft = 'Project: ' + $('#mob-' + uuid + ' div div  .project-name').html()...

這里 :

modalContentLeft  = 'Project: ' + $('#mob-' + uuid + ' div.project-name').html();

然后,您的 html 是:

<div class="mobile-item-row-first first-row clearfix" id="mob-6c985947-c68a-4f5b-9ebe-0d488b696f0d">
  <div class="project-client pull-left">
    <i class="ion-ios-people ion-fw" data-original-title="" title=""></i>&nbsp; Sienna Cremin
  </div>
</div>

這不反映您的 jquery 選擇器:

$('#mob-' + uuid + ' div div  .project-name').html()

似乎您關閉了太多 div,因此您的 .project-name div 不在 #mob-id div 中。

$(elem).html()將獲得 div 的所有內容(包括<i></i>部分)。 如果你只想要文本,你可以去$(elem).text()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM