[英]javascript/jquery - How to take variable of child class name from repeated occurrences of parent class
EDIT (old link to JSFiddle was wrong): Link to JSFiddle example: https://jsfiddle.net/uvexys0a/ 编辑(到JSFiddle的旧链接是错误的):链接到JSFiddle示例: https ://jsfiddle.net/uvexys0a/
I am attempting to program in jQuery so it wraps an HTML link to the staff member's profile page wraps around the entire each div with the class name staffList
. 我正在尝试使用jQuery编程,因此它包装了指向工作人员资料页面的HTML链接,并用类名
staffList
环绕了整个每个div。 The path to the page is stored as a child class in each div, as seen on the JSFiddle. 页面的路径作为子类存储在每个div中,如JSFiddle所示。
The code seems to function, somewhat. 该代码似乎起作用了。 Both links end up going to John Smith's profile:
这两个链接最终都转到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>
However, if the code was running properly, it would output like this: 但是,如果代码运行正常,它将输出如下:
<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>
How do you code so the variable staffURL
changes with each repeated parent div with parent class staffList
and the child class the corresponding staff member's link? 如何编写代码,使变量
staffURL
在每个重复的父div(父类staffList
和子类对应的职员链接)中更改?
You're basing your links off of the second class name, but in your second staffList, you say John Smith again, so you get john-smith both times for each link. 您是基于第二类的名称创建链接,但是在第二个StaffList中,您再次说了John Smith,因此每个链接两次都获得john-smith。 You can change that to jane-smith and loop over each item to get what you want.
您可以将其更改为简史密斯,然后遍历每个项目以获得所需的内容。 Try this:
尝试这个:
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: https ://jsfiddle.net/7nxbu1t5/2/
You'd need to loop through each staffList
item in order to set the URL dynamically. 您需要遍历每个
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.