简体   繁体   English

.each js导致页面上其他类的问题

[英].each js causing issues with other classes on page

HTML HTML

<ul class="courseDates">
    <li class="dateOne col-sm-2">
        {tag_course date 1}
    </li>
    <li class="dateTwo col-sm-2">
        {tag_course date 2}
    </li>
    <li class="dateThree col-sm-2">
        {tag_course date 3}
    </li>
    <li class="dateFour col-sm-2">
        {tag_course date 4}
    </li>
    <li class="dateFive col-sm-2">
        {tag_course date 5}
    </li>
</ul>


Javascript 使用Javascript

$('.dateOne').each(function() {    
    var data =jQuery(this).text();
    var arr = data.split('-');
    jQuery(this).html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date 1}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> <span class="day">'+arr[0] + '</span> <span class="month">'+arr[1] + '</span> <span  class="year">'+arr[2] + '</span> </a>');            
}); 

$('.dateTwo').each(function() {    
    var data =jQuery(this).text();
    var arr = data.split('-');
    jQuery(this).html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date 2}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> <span class="day">'+arr[0] + '</span> <span class="month">'+arr[1] + '</span> <span  class="year">'+arr[2] + '</span> </a>');            
}); 

$('.dateThree').each(function() {    
    var data =jQuery(this).text();
    var arr = data.split('-');
    jQuery(this).html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date 3}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> <span class="day">'+arr[0] + '</span> <span class="month">'+arr[1] + '</span> <span  class="year">'+arr[2] + '</span> </a>');            
});

$('.dateFour').each(function() {    
    var data =jQuery(this).text();
    var arr = data.split('-');
    jQuery(this).html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date 4}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> <span class="day">'+arr[0] + '</span> <span class="month">'+arr[1] + '</span> <span  class="year">'+arr[2] + '</span> </a>');            
}); 

$('.dateFive').each(function() {    
    var data =jQuery(this).text();
    var arr = data.split('-');
    jQuery(this).html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date 5}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> <span class="day">'+arr[0] + '</span> <span class="month">'+arr[1] + '</span> <span  class="year">'+arr[2] + '</span> </a>');            
}); 

I need to remove the .each so the code operates for each item, the .each is causing undefined errors in other items on the page. 我需要删除.each以便代码对每个项目进行操作,.each会在页面上的其他项目中导致未定义的错误。 It needs to be individual, so it outputs specific tags per group of dates, the .each function make the last item on the page work, but then has undefined errors on the first item . 它必须是单独的,因此它为每组日期输出特定标签, .each函数使页面上的最后一项工作,但在第一项上有未定义的错误。

Here is a working version, so you can check it. 是一个工作版本,所以你可以检查它。

Is there a better way to write the JS? 有没有更好的方法来编写JS?

Any help regarding this matter would be greatly appreciated, thanks. 非常感谢有关此事的任何帮助,谢谢。

Try something like this. 尝试这样的事情。

HTML HTML

<ul class="courseDates">
    <li class="date col-sm-2">
        {tag_course date 1}
    </li>
    <li class="date col-sm-2">
        {tag_course date 2}
    </li>
    <li class="date col-sm-2">
        {tag_course date 3}
    </li>
    <li class="date col-sm-2">
        {tag_course date 4}
    </li>
    <li class="date col-sm-2">
        {tag_course date 5}
    </li>
</ul>

Javascript 使用Javascript

$('.date').each(function(index) {    
    var data =jQuery(this).text();
    var arr = data.split('-');
    jQuery(this).html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date ' + index + '}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> '+arr[0] + ' '+arr[1] + ' '+arr[2] + ' </a>');           
});

You're simply overcomplicating this, just make them all have one shared class (eg date ) and run the .each code just once. 你只是过于复杂,只需让它们都有一个共享类(例如date )并只运行一次.each代码。 They all do the same behaviour which is 他们都做同样的行为

  • Split the date in the li by a hyphen 用连字符分隔li的日期
  • use the date part as a class on the <button> 使用日期部分作为<button>上的类
  • Wrap the date with a link. 用链接包裹日期。

See working code below and view source to see the transformation. 请参阅下面的工作代码并查看源代码以查看转换。

 $('.date').each(function() { var $this = $(this); var data =$this.text(); var arr = data.split('-'); $this.html('<a class="button '+arr[1] + '" href="/training-rego?trainingDate={tag_course date 5}&trainingName={tag_name_nolink}&courseId={tag_itemid}"> '+arr[0] + ' '+arr[1] + ' '+arr[2] + ' </a>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="courseDates"> <li class="date col-sm-2"> 14-Dec-2016 </li> <li class="date col-sm-2"> 15-Dec-2016 </li> <li class="date col-sm-2"> 16-Dec-2016 </li> <li class="date col-sm-2"> 24-Dec-2016 </li> <li class="date col-sm-2"> 29-Dec-2016 </li> </ul> 

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

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