[英]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.