簡體   English   中英

如何使用Jquery和類選擇器獲取元素的第一個子文本?

[英]How to Get A Element First Child Text Using Jquery And Class Selector?

我在元素中有兩個元素。 像下面=>

<div class="NDateCount" style="display:inline-block">
     <!--Issue to solve-->
     <small style="display:none;" class="ng-binding">2017-07-08T12:44:10.367+06:00</small>
     <small ng-show="CountPostDate(post.timelineStrore.creationDate) < 2" style="color:#1ab394;" class="ng-binding"><i class="fa fa-clock-o"></i> 3 hours ago</small>
     <!--End of Issue to solve-->
</div>

我有這種div超過10次與動態生成。 我想要div元素的第一個孩子是small文本; 我到目前為止已經完成==>

    var ReloadTime = function () {
        $('.NDateCount').each(function () {
            var OldValue = $(this).first().text();
            alert(OldValue);
         });
        setTimeout(ReloadTime, 50);
    }

但是上面的函數提供了類似small標簽內部文本的功能。 像==> 結果

有沒有解決這個問題的方法……

像這樣

var ReloadTime = function () {
    $('.NDateCount').each(function () {
        var OldValue = $(this).find('small').eq(0).text();
        alert(OldValue);
     });
    setTimeout(ReloadTime, 50);
}

它將為您工作。

first()返回div元素,因此您可以在其中獲取文本。

獲取children$(this) ,然后使用first()

var OldValue = $(this).children().first().text();

 var ReloadTime = function() { $('.NDateCount').each(function() { var OldValue = $(this).children().first().text(); alert(OldValue); }); //setTimeout(ReloadTime, 50); } ReloadTime(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="NDateCount" style="display:inline-block"> <!--Issue to solve--> <small style="display:none;" class="ng-binding">2017-07-08T12:44:10.367+06:00</small> <small ng-show="CountPostDate(post.timelineStrore.creationDate) < 2" style="color:#1ab394;" class="ng-binding"><i class="fa fa-clock-o"></i> 3 hours ago</small> <!--End of Issue to solve--> </div> <div class="NDateCount" style="display:inline-block"> <!--Issue to solve--> <small style="display:none;" class="ng-binding">2017-07-08T12:44:10.367+06:00</small> <small ng-show="CountPostDate(post.timelineStrore.creationDate) < 2" style="color:#1ab394;" class="ng-binding"><i class="fa fa-clock-o"></i> 3 hours ago</small> <!--End of Issue to solve--> </div> 

 var ReloadTime = function () { $('.NDateCount').each(function () { var OldValue = $(this).find('small').first().text(); alert(OldValue); }); } setTimeout(ReloadTime, 50); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="NDateCount" style="display:inline-block"> <!--Issue to solve--> <small style="display:none;" class="ng-binding">2017-07-08T12:44:10.367+06:00</small> <small ng-show="CountPostDate(post.timelineStrore.creationDate) < 2" style="color:#1ab394;" class="ng-binding"><i class="fa fa-clock-o"></i> 3 hours ago</small> <!--End of Issue to solve--> </div> 

首先,您正在做的主要錯誤是setTimeout()函數位於ReloadTime()函數內部。 因此,除非您一次觸發該函數,否則setTimeout()不會調用該函數。 所以我不同意以上答案。 其次,在each函數引用中使用$(this)引用單個$('.NDateCount')元素,以便實現所需的功能,首先需要在$(this)下找到元素<small> ,然后獲取first()元素,最后是text()

暫無
暫無

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

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