【问题标题】:javascript/jquery - How to take variable of child class name from repeated occurrences of parent classjavascript/jquery - 如何从重复出现的父类中获取子类名称的变量
【发布时间】:2019-03-06 15:50:30
【问题描述】:

编辑(到 JSFiddle 的旧链接是错误的):链接到 JSFiddle 示例:https://jsfiddle.net/uvexys0a/

我正在尝试使用 jQuery 进行编程,因此它会包含一个指向工作人员个人资料页面的 HTML 链接,并使用类名 staffList 环绕整个每个 div。页面路径作为子类存储在每个 div 中,如 JSFiddle 所示。

代码似乎在某种程度上起作用。两个链接最终都指向 John Smith 的个人资料:

<a href="https://example.com/john-smith">
    <div class="staffList john-smith">
        <p>John Smith</p>
        <p>Co-Founder</p>
    </div>
</a>

<a href="https://example.com/john-smith">
    <div class="staffList jane-smith">
        <p>Jane Smith</p>
        <p>Co-Founder</p>
    </div>
</a>

但是,如果代码运行正常,它会输出如下:

<a href="https://example.com/john-smith">
    <div class="staffList john-smith">
        <p>John Smith</p>
        <p>Co-Founder</p>
    </div>
</a>

<a href="https://example.com/jane-smith">
    <div class="staffList jane-smith">
        <p>Jane Smith</p>
        <p>Co-Founder</p>
    </div>
</a>

您如何编码,使变量staffURL 随父类staffList 和子类对应工作人员链接的每个重复父div 而变化?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您的链接基于第二个班级名称,但在您的第二个工作人员列表中,您再次说 John Smith,因此每个链接都两次获得 john-smith。您可以将其更改为 jane-smith 并遍历每个项目以获得您想要的。试试这个:

jQuery(function($){
  var staffList = $(".staffList");
  
  $.each(staffList, function(i) {
    var staffURL = $(this).attr('class').split(' ')[1];
    $(staffList[i]).wrap("<a href='https://example.com/"+staffURL+"/'></a>");
  });
  
});
.staffList {
  border: 1px solid #000;
  margin: 15px;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <div id="warpper">
    <div id="staffSection">
      <div class="staffList john-smith">
        <p>John Smith</p>
        <p>Co-Founder</p>
      </div>
      <div class="staffList jane-smith">
        <p>Jane Smith</p>
        <p>Co-Founder</p>
      </div>
    </div>
  </div>
</div>

jsfiddle:https://jsfiddle.net/7nxbu1t5/2/

【讨论】:

  • 那是一个旧链接,代表我的错误。我将帖子编辑为新链接(与您所说的有关),但它仍然只链接约翰史密斯。 JSFiddle:jsfiddle.net/uvexys0a 你添加到 jQuery 的循环有帮助。
【解决方案2】:

您需要遍历每个 staffList 项目才能动态设置 URL。

jQuery(function($) {

  /**
   * Loop through each list item
   */
  $('.staffList').each(function() {
    var $listItem = $(this);

    var staffSlug = $listItem
      .attr('class') // Get the value of the class attribute
      .replace('staffList', '') // Remove the common class
      .trim(); // Clear up any pre/appending white space

    // Wrap element in `a` tag
    $listItem.wrap('<a href="https://example.com/' + staffSlug + '"></a>');
  });

});
.staffList {
  border: 1px solid #000;
  margin: 15px;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <div id="warpper">
    <div id="staffSection">

      <div class="staffList john-smith">
        <p>John Smith</p>
        <p>Co-Founder</p>
      </div>

      <div class="staffList jane-smith">
        <p>Jane Smith</p>
        <p>Co-Founder</p>
      </div>

    </div>
  </div>
</div>

【讨论】:

  • 答案似乎有效,但是,为了让它工作,我必须使用字符串 split() 方法
猜你喜欢
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2018-08-03
  • 1970-01-01
  • 2016-06-25
相关资源
最近更新 更多