简体   繁体   English

jQuery不会在首次点击时加载内容

[英]Jquery doesn't load content on first click

I have a short script which loads content of a given link into an element. 我有一个简短的脚本,可将给定链接的内容加载到元素中。 The content depends on the id of the link clicked. 内容取决于单击的链接的ID。 The problem I'm facing is that it never loads the content on the first click. 我面临的问题是,它永远不会在第一次单击时加载内容。 I need to click on another main link and only then it loads. 我需要单击另一个主链接,然后才加载。 Let you show you what I've got: 让您告诉我我所拥有的:

<div class="mainTabs">
<ul>
  <li class="activeTab"><a href="#skills">Skills</a></li>
  <li id="secondTab"><a href="#certificates">Certificates</a></li>
</ul>
<div class="tabsContent">
  <div id="skills" class="tab active">
    {% include "./_skills.html" %}
  </div>
  <div class="tab" id="certificates">
    {% include "./_certsList.html" %}
  </div>
</div>

And Jquery is: jQuery是:

  $(document).ready(function(){
$('.textBox h3').load("{% url 'aboutme' %}");
$('.row li').on('click', function(e){
  e.preventDefault();
  $('.activeSkill').removeClass('activeSkill');
  $(this).find('a').addClass('activeSkill');
  var link = $(this).find('a').attr('href');
  $('.textBox h3').load(link);
});
});

Let's say I click on 'Certificates'. 假设我点击“证书”。 The page below is not loaded at first, only if I click on another link then it loads. 仅当我单击另一个链接后,才开始加载下面的页面。 What could be the problem here? 这可能是什么问题?

Im struggling to understand this without a full example, but my interpretation of this is that the logic appears to be wrong : 我在没有完整示例的情况下难以理解,但是我对此的解释是逻辑似乎是错误的:

The following line removes all 'activeSkill' classes from the DOM. 以下行从DOM中删除了所有“ activeSkill”类。

$('.activeSkill').removeClass('activeSkill');

The next line adds the 'activeSkill' class to the <a> under the <li> that was clicked... was that intended? 下一行将'activeSkill'类添加到所单击的<li>下的<a>

$(this).find('a').addClass('activeSkill');

Can you explain what the role of activeSkill is? 您能解释一下activeSkill的作用是什么? Also shouldn't you be setting the active class on the selected tab <div> , and removing it from the currently active <div> ? 另外,您是否不应该在选定的选项卡<div>上设置active类,并将其从当前活动的<div>删除?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM