【问题标题】:I am unable to get the parent element of the anchor tag我无法获取锚标记的父元素
【发布时间】:2019-08-20 09:50:49
【问题描述】:

HTML 代码:

<ul class="nav nav-tabs">
    <li role="presentation" id="tab1" class="active"><a onclick="switchTabs(1)">Tab1</a></li>
    <li role="presentation" id="tab2"><a onclick="switchTabs(2)">Tab2</a></li>
</ul>

JS代码:点击时没有出错,但没有切换

function switchTabs(idx) {
    // this is not working
    var $li = $(this).parent();
    if($li.hasClass('active')) {
        return false; // not enter here
    }
    $li.removeClass('active');
    if (idx === 1) {
        $li.next().addClass('active');
    } else {
        $li.prev().addClass('active');
    }

    // but this works while using id selector
    /*if (idx === 1) {
        $("#tab1").addClass('active');
        $("#tab2").removeClass('active');
    } else if(idx === 2) {
        $("#tab2").addClass('active');
        $("#tab1").removeClass('active');
    }*/
    return false;
}

可能是什么原因?

【问题讨论】:

  • 你可以通过$(this)switchTabs(1)
  • 您在调用函数时没有定义this。最好使用 jQuery 分配侦听器,而不是在模板中使用 onclick。然后,jQuery 会为你做这部分。

标签: javascript jquery html


【解决方案1】:

尝试这种方法,我认为这将是您的案例的更好解决方案

<html>
<head>
    <meta charset="utf-8"/>
</head>

<style>
    .active {
        color: red;
    }
</style>

<body>
<ul class="nav nav-tabs">
    <li role="presentation" id="tab1" class="active"><a onclick="switchTabs(this)">Tab1</a></li>
    <li role="presentation" id="tab2"><a onclick="switchTabs(this)">Tab2</a></li>
</ul>
</body>

<script
              src="https://code.jquery.com/jquery-3.4.1.js"
              integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
              crossorigin="anonymous"></script>

<script>

function switchTabs(elm) {
    var $li = $(elm).parent();
    if(!$li.hasClass('active')) {
        $li.addClass('active');
        $li.siblings().removeClass('active');
    }
}

</script>

【讨论】:

  • 谢谢!它完成了这项工作。
【解决方案2】:

将元素发送到函数中,然后您将拥有它的父元素:

<a onclick="switchTabs(this,1)">

然后

function switchTabs(elm,idx) {
// this is not working
  var $li = $(elm).parent();
  // Rest of your code...

【讨论】:

    【解决方案3】:

    检查此方法以使其更容易(并使用$(this)

    $(document).on('click','.nav li a', function(){                                      
      let tab = $(this).data('tab');//$(this) is the a clicked.                                      
      $('.nav li.active').removeClass('active');//hide active Tab
      $('.nav li#tab'+tab).addClass('active');//show Tab
    })
    .active{font-size:20px;color:red}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul class="nav nav-tabs">
        <li role="presentation" id="tab1" class="active"><a data-tab="1">Tab1</a></li>
        <li role="presentation" id="tab2"><a data-tab="2">Tab2</a></li>
    </ul>

    【讨论】:

      【解决方案4】:

      您仅将索引而不是此对象传递给 switchTab 函数。您可以利用 idx 获取点击的 li,然后将逻辑添加/删除活动类。

      见下方代码

      function switchTabs(idx) {
          // this is not working
          //var $li = $(this).parent(); -- you are not passing this anywhere in the function hence it is not getting
          $('li').removeClass('active'); // remove all active 
          var $li = $('#tab' + idx).addClass('active'); // use idx to create tab id of clicked li and add active class
      }
      .active {
        color: red;
      }
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
      <ul class="nav nav-tabs">
          <li role="presentation" id="tab1" class="active"><a onclick="switchTabs(1)">Tab1</a></li>
          <li role="presentation" id="tab2"><a onclick="switchTabs(2)">Tab2</a></li>
      </ul>

      【讨论】:

        猜你喜欢
        • 2020-03-04
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多