자바스크립트 explorer 호환

자바스크립트 explorer 호환

QA

자바스크립트 explorer 호환

답변 1

본문

안녕하세요. 
자바스크립트 왕초보로 학습자로서 며칠을 고생해 코드를 완성했다고 생각하고 좋아했는데.
Internet Explorer 에서 안되네요...ㅠㅠ 절망감..

 


jQuery(function($){
        function monthYearShow(){
           
            $(".only_one").each(function() {
                this.remove();
            });
           
            var entYear_arr=[];
             $('input[name="ent_date_each"]').each(function(){entYear_arr.push($(this).val().slice(0,4))});
            var entMonth_arr=[];
             $('input[name="ent_date_each"]').each(function(){entMonth_arr.push($(this).val().slice(5,7).replace(/(^0+)/, ""))});
            //console.log(entYear_arr);

            var today = new Date();
            for(var i=0; i<entYear_arr.length; i++){                
                if( i==0 ){
                    if( entYear_arr[0] != today.getFullYear() ){
                        $(".ent_posts div").eq(i).before("<span class='only_one' style='background:yellow;'>"+entYear_arr[i]+"년 </span>");
                    }
                    $(".ent_posts div").eq(i).before("<span class='only_one' style='background:yellow;'>"+entMonth_arr[i]+"월</span>");
                }                
                else{
                    if( entMonth_arr[i] != entMonth_arr[i-1] ){
                        if( entYear_arr[i] != entYear_arr[i-1] ){
                            $(".ent_posts div").eq(i).before("<span class='only_one'  style='background:yellow;'>"+entYear_arr[i]+"년 </span>");
                        }
                        $(".ent_posts div").eq(i).before("<span class='only_one' style='background:yellow;'>"+entMonth_arr[i]+"월</span>");
                    }
                }
            }
          }
        $(window).load(monthYearShow);
});

일단 remove()가 작동 안되네요. 그리고 다른 것도 작동 안하는 것 같은데.ㅠ 먼지 모르겠네요. 일단 개발자 창에는 remove만 안된다고 뜨는데, 

ajax 시 년월이 추가가 안되는 것을 보면. 다른 것도 안되는 것이 있는 것 같은데, 

조언을 좀 부탁드립니다...ㅠ 절망감이 크네요ㅠ

이 질문에 댓글 쓰기 :

답변 1

"jQuery(function($){~ });" 이, 문서가 로드될 때 자동으로 실행됩니다.

그래서 monthYearShow 를 만들고 $(window).load() 를 호출하지 않아도 됩니다.

 

this 보다는 jqeury $(this) 사용을 추천합니다.

생각난 것들만 정리해서, 별로 도움이 되지 않을 수도 있습니다.

 


jQuery(function($){
        
           
  $(".only_one").each(function() {
      //this.remove();
      $(this).remove();
  });
 
  var entYear_arr=[];
   $('input[name="ent_date_each"]').each(function(){entYear_arr.push($(this).val().slice(0,4))});
  var entMonth_arr=[];
   $('input[name="ent_date_each"]').each(function(){entMonth_arr.push($(this).val().slice(5,7).replace(/(^0+)/, ""))});
  //console.log(entYear_arr);
  var today = new Date();
  for(var i=0; i<entYear_arr.length; i++){                
      if( i==0 ){
          if( entYear_arr[0] != today.getFullYear() ){
              $(".ent_posts div").eq(i).before("<span class='only_one' style='background:yellow;'>"+entYear_arr[i]+"년 </span>");
          }
          $(".ent_posts div").eq(i).before("<span class='only_one' style='background:yellow;'>"+entMonth_arr[i]+"월</span>");
      }                
      else{
          if( entMonth_arr[i] != entMonth_arr[i-1] ){
              if( entYear_arr[i] != entYear_arr[i-1] ){
                  $(".ent_posts div").eq(i).before("<span class='only_one'  style='background:yellow;'>"+entYear_arr[i]+"년 </span>");
              }
              $(".ent_posts div").eq(i).before("<span class='only_one' style='background:yellow;'>"+entMonth_arr[i]+"월</span>");
          }
      }
  }
          
    //$(window).load(monthYearShow);
});

답변을 좀 늦게 봤네요. 빨리 봤더라면 고생을 덜 했을텐데..ㅠ 말씀하신대로 this를 $(this)로 바꾸니까 모든 문제가 해결되었습니다. 아 그리고 말씀대로 $(window).load()는 안하고 함수만 실행해도 되는군요. 감사합니다. 즐거운 하루 되세요.

참고로..
jQuery(function($){
});
위 코드가  jquery $(document).ready 의 축약형입니다. ($ = jQuery) https://learn.jquery.com/using-jquery-core/document-ready/

요즘에는 더 간단하게
$(function() {
}); 으로 많이 쓰이는 추세입니다. https://zetawiki.com/wiki/JQuery_.ready()

문서 로드가 완료될 때 실행됩니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 4
© SIRSOFT
현재 페이지 제일 처음으로