[英]How to find an attribute value for some Element in a Class with jQuery?
I need help guys, i have a class .content
, this class have attribute called data-time
, i want to get the value from data-time
attribute, after that i want to show this value in .content
class. 我需要帮助人员,我有一个
.content
类,该类具有名为data-time
属性,我想从data-time
属性中获取该值,之后我想在.content
类中显示此值。 But my code is not work better. 但是我的代码效果不好。
My HTML Code 我的HTML代码
<div class="content first" data-time="200"> </div>
<div class="content second" data-time="300"> </div>
<div class="content third" data-time="400"> </div>
<div class="content fourth" data-time="500"> </div>
My jQuery Code 我的jQuery代码
var time = $(this).find('.content').attr('data-time');
$(this).find('.content').text(time);
Please anybody to help me. 请任何人帮助我。
try use .each to iterate 尝试使用.each进行迭代
$(".content", this ).each(function(){
$(this).text( $(this).attr("data-time") );
});
See in fiddle -> http://jsfiddle.net/Castrolol/vAnmf/ 参见小提琴-> http://jsfiddle.net/Castrolol/vAnmf/
Try this one..It will output your attribute text with no extra spaces at start and end of the string 试试这个。它将输出属性文本,字符串的开头和结尾都没有多余的空格
$(".content").each(function () {
$(this).text($.trim($(this).attr("data-time")));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.