[英]Extract value from inner attribute of Html
I am learning web development and this is my first questions. 我正在学习Web开发,这是我的第一个问题。 Sorry if I ask in unstructured manner. 抱歉,如果我以非结构化的方式询问。
I am using flexslider in my project and I want to extract some values from slider. 我在项目中使用了flexslider,我想从滑块中提取一些值。
<ul class="position-list slides background-white">
<li id="172" class="slide pointer season-selector active" data-short-year="17" data-short-desc="FW17"></li>
<li id="173" class="slide pointer season-selector active" data-short-year="18" data-short-desc="SS18"></li>
I want to get data-short-desc
values. 我想获取data-short-desc
值。 I tried .find
to get values but it doesn't work. 我尝试.find
来获取值,但它不起作用。
$(".position-list").find('data-short-year').val();
Is there any way to get those values? 有没有办法获得这些价值?
You can try following:- 您可以尝试以下操作:
$('.position-list li').each(function(){
var short_year = $(this).data('short-year');
});
data-short-year
is an attribute, not an element. data-short-year
是属性,而不是元素。
You need an attribute selector: .find('[data-short-year]')
您需要一个属性选择器: .find('[data-short-year]')
Then you are looking to get the content of the attribute, not the current value of a form control. 然后,您正在寻找获取属性的内容,而不是表单控件的当前值。
Normally, you would use attr()
, but data-*
is special: .data('short-year')
. 通常,您将使用attr()
,但是data-*
是特殊的: .data('short-year')
。
I get a JSON and then on the value I get, I am trying to sort them 我得到一个JSON,然后根据获得的值对它们进行排序
This is different to the title of your question. 这与您的问题的标题不同。 To achieve this you can use the sort()
method: 为此,您可以使用sort()
方法:
$('.position-list li').sort(function(a, b) { return $(a).data('short-year') > $(b).data('short-year'); }).appendTo('.position-list');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="position-list slides background-white"> <li id="173" class="slide pointer season-selector active" data-short-year="18" data-short-desc="SS18"> SS18 - 18 </li> <li id="172" class="slide pointer season-selector active" data-short-year="12" data-short-desc="Bar12"> Bar - 12 </li> <li id="172" class="slide pointer season-selector active" data-short-year="10" data-short-desc="Foo10"> Foo - 10 </li> <li id="172" class="slide pointer season-selector active" data-short-year="17" data-short-desc="FW17"> FW17 - 17 </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.