使用jQuery基于属性查找ahref值

时间:2018-09-21 18:10:34

标签: jquery html

当列表中有ahref所属的活动属性时,我试图查找aherf值

<ul class="nav nav-tabs " id="segmentTabs" role="tablist">
  <li class="nav-item active">
    <a class="nav-link" id="segment-tab" data-toggle="tab" href="#segment" role="tab" aria-controls="#engagement" aria-selected="false" data-value="segments"></a>
  </li>
</ul>

这是我尝试使用jQuery的方法。我在undefined中得到了value

var $segmentTabs = $('#segmentTabs');

function exportClientCharts() {
  var $controlButtons = $segmentTabs.find('.active');
  var value = $controlButtons.data('value');
  if (value === Enums.dashboardTabs.SEGMENTS) {
    SegmentCountsChart.exportClientCharts();
  }
}

2 个答案:

答案 0 :(得分:1)

$controlButtons.find('a').attr('href');

答案 1 :(得分:0)

data-valuea内的li.active上,因此您需要在find()中更改选择器:

var $segmentTabs = $('#segmentTabs');

function exportClientCharts() {
  var $controlButtons = $segmentTabs.find('.active a'); // note the addition of 'a' here
  var value = $controlButtons.data('value');

  if (value === Enums.dashboardTabs.SEGMENTS) {
    SegmentCountsChart.exportClientCharts();
  }
}