标题的选择器结果为undefined

时间:2015-09-15 06:04:57

标签: javascript jquery jquery-selectors

sample

<span class="ownername" id="fname" data-class="ownername" title="072-219-461-000">
Consuelo Delos Reyes Gonzales ,
</span>

使用

获取此跨度的标题
$('#af_rpta_listoflandref_mchns').find("tr:not(:eq(0))");
var $tds = $(this).find('td');

alert($tds.eq(2).find('span.ownername').find('#fname').attr('title'));

undefined

中的结果

4 个答案:

答案 0 :(得分:1)

编辑:

var $trs = $('#af_rpta_listoflandref_mchns').find("tr").eq(1);
var $span = $($trs).find('td > span.ownername');

alert($span.attr('title'));

答案 1 :(得分:1)

here

var $rows1 = $('#af_rpta_listoflandref_mchns').find("tr:not(:eq(0))");

$rows1.each(function () {
    var $tds = $(this).find('td');
    alert($tds.eq(2).find("span#fname.ownername").data('id'));
});

遍历表格。此外,我将标题更改为数据attr命名为id

答案 2 :(得分:0)

id在您的文档中应该是唯一的。因此,在这种情况下,$('#fname').attr('title')会为您提供所需的标题。

但根据您对Amey Deshpandes的评论,我猜测会有更多行,跨度更大。

所以你有两个选择:

  • id更改为class
  • 为元素添加唯一的id(例如id="fname1")。

从那时起,您还需要调整选择器:

/* with class*/
$('.fname').each( function() {
   var title = $(this).attr('title');
   alert( title );
});

/* with id + counter */
$('[id^=fname]').each( function() {
   var title = $(this).attr('title');
   alert( title );
});

/* based on it's position */
$('#af_rpta_listoflandref_mchns tbody tr:not(:first)').each( function() {
    var title = $(this).find('td:nth-child(3) span').attr('title');
    alert( title );
});

答案 3 :(得分:0)

您可以尝试以下操作:

$("tr").each(function() {

    v = $(this).find("td:nth-child(3) span.ownername").attr("title"); 

});

alert(v);