jquery 每个函数将所有内容添加到全局变量,而不是每个循环添加1个

ou6hu8tu  于 9个月前  发布在  jQuery
关注(0)|答案(1)|浏览(92)

基本上我有一个每个功能如下

function staffProfileTechnology() {
    $('.staff-profile').addClass('staffProfileTechnology');

    var staffTechnologyBioContent = $('.staffProfileTechnology').find('#ctl00_PlaceHolderMain_ctl07__ControlWrapper_RichHtmlField').html();

    $('.staffProfileTechnology .col-sm-9 #accordion h3').each(function(){

        var staffTechnologyAccordionTitle = $(this).text();
        var staffTechnologyAccordionContent = $(this).closest('#accordion').find('.ms-rtestate-field').html();
        
        console.log(staffTechnologyAccordionTitle+':'+staffTechnologyAccordionContent+',');
    });
};

字符串
在console中,我看到'staffTechnologyAccordionTitle'出现了一次,但是对于'staffTechnologyAccordionContent',我有相同的内容,而不是它移动到下一个迭代,标题正确。
所以我得到了一个新的标题,但内容相同,如下所示。

Teaching : <p>​For 6 years Jon was the principal lecturer in Control Engineering at the RAF College Cranwell where he taught post graduate engineering officers up to MSc. Within his own company, Muretex, he designed and delivered a module on Avionic Systems for 3rd students on the Aerospace Engineering course at the University of Leicester. As Dean of the school h​e oversees the broadening of the school of technologies portfolio of programmes to include Integrated Masters courses in engineering and Degree Apprenticeships as well as additional computer science related courses.&nbsp;<br></p>

Research : <p>​For 6 years Jon was the principal lecturer in Control Engineering at the RAF College Cranwell where he taught post graduate engineering officers up to MSc. Within his own company, Muretex, he designed and delivered a module on Avionic Systems for 3rd students on the Aerospace Engineering course at the University of Leicester. As Dean of the school h​e oversees the broadening of the school of technologies portfolio of programmes to include Integrated Masters courses in engineering and Degree Apprenticeships as well as additional computer science related courses.&nbsp;<br></p>


这就是控制台中的代码

<div id="accordion">
<h3>Teaching</h3>
<div class="ms-rtestate-field"><p>​For 6 years Jon was the principal lecturer in Control Engineering at the RAF College Cranwell where he taught post graduate engineering officers up to MSc. Within his own company, Muretex, he designed and delivered a module on Avionic Systems for 3rd students on the Aerospace Engineering course at the University of Leicester. As Dean of the school h​e oversees the broadening of the school of technologies portfolio of programmes to include Integrated Masters courses in engineering and Degree Apprenticeships as well as additional computer science related courses.</p></div>

<h3>Research</h3>
<div class="ms-rtestate-field"><p>​Jon has had commercial success with his own company Muretex, winning significant, nationally competed, research grant funding; including Innovate UK funding for Robotics and Autonomous Systems. He has international contacts and reach, having proposed and chaired 2 European research action groups over 8 years and been invited to deliver a NATO lecture series on autonomy. His current research interests lie at the intersection of systems engineering practices and the capture of requirements for human-machine teaming in autonomous systems to ensure that levels of autonomy are appropriate to meet overall system needs.</p></div>

j9per5c4

j9per5c41#

看起来你想为每个<h3>找到.ms-rtestate-fieldimmediate next 示例。目前你正在做的是遍历到一个公共的父示例并找到 * 所有 * 示例:

$(this).closest('#accordion').find('.ms-rtestate-field')

字符串
但是如果this表示<h3>元素,那么你可以使用类似.next()的东西来查找下一个匹配的元素。例如:

$('#accordion h3').each(function(){
  var staffTechnologyAccordionContent = $(this).next('.ms-rtestate-field').html();
  console.log(staffTechnologyAccordionContent);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="accordion">
  <h3>Teaching</h3>
  <div class="ms-rtestate-field">
    <p>For 6 years Jon was the principal lecturer...</p>
  </div>
  <h3>Research</h3>
  <div class="ms-rtestate-field">
    <p>Jon has had commercial success with...</p>
  </div>
</div>

的数据

相关问题