Jquery,如何通过属性的第一个位置值选择元素

时间:2016-07-06 12:06:49

标签: javascript jquery arrays

在html中:

ListView lv = (ListView) convertView.findViewById(R.id.ExceedingList);

我试图构建一个选择器,以便通过data-s属性的第一个位置的值获取trs。

提前致谢。

5 个答案:

答案 0 :(得分:2)

根据我的理解,您可以通过data-s属性的第一个位置获取元素。

$('[data-s^="3 "]');

或者如果你想获得data-s属性值的第一个位置,你可以这样做;

$('[data-s]').data('s').split(' ')[0];

你有a plunker example

答案 1 :(得分:1)

您可以使用.split(" ")[0]获取data-s属性的第一个值



$("table tr").each(function() {
    console.log($(this).attr("data-s").split(" ")[0])
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<table>
<tr data-s=""></tr>
<tr data-s="1"></tr>
<tr data-s="2 1"></tr>
<tr data-s="3 2 1"></tr>
</table>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我的提议基于jQuery.filter函数:

$(function () {
  var elements = $('table tr').filter(function(index, element) {
    return this.getAttribute('data-s').split(' ')[0] == '1';
  });
  console.log(elements.length);
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<table>
    <tr data-s=""></tr>
    <tr data-s="1"></tr>
    <tr data-s="2 1"></tr>
    <tr data-s="3 2 1"></tr>
</table>

答案 3 :(得分:1)

$('[data-s]').each(function () {
    var test = $(this).attr('data-s')
    if(test != undefined){
        var firstDataValue = parseInt(test.split(' ')[0]);
        /*run you test case for first value and return back this on success */
    }
})

答案 4 :(得分:1)

function customFn(elm,sel)
{
  var tmp = $();
  $(elm).each(function(){
    data=$(this).data('s').toString().split(' ')[0];
    if(data.trim()===sel.toString().trim())
     tmp=tmp.add(this);
  });
  return tmp
}

customFn("tr","3");

https://jsfiddle.net/68x8Leuz/

相关问题