테스트 사이트 - 개발 중인 베타 버전입니다

자바스크립트 explorer 호환 채택완료

MrNothing 7년 전 조회 2,150

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

 

</p>

<p>jQuery(function($){</p>

<p>        function monthYearShow(){</p>

<p>           

            $(".only_one").each(function() {

                this.remove();

            });</p>

<p>           

            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);</p>

<p>

            var today = new Date();</p>

<p>            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>");

                    }

                }

            }

          }</p>

<p>        $(window).load(monthYearShow);</p>

<p>});</p>

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

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

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

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
7년 전

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

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

 

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

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

 

</p>

<p>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);</p>

<p>  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);

});</p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 3개

M
MrNothing
7년 전
답변을 좀 늦게 봤네요. 빨리 봤더라면 고생을 덜 했을텐데..ㅠ 말씀하신대로 this를 $(this)로 바꾸니까 모든 문제가 해결되었습니다. 아 그리고 말씀대로 $(window).load()는 안하고 함수만 실행해도 되는군요. 감사합니다. 즐거운 하루 되세요.
마르스컴퍼니
7년 전
참고로..
jQuery(function($){
});
위 코드가 jquery $(document).ready 의 축약형입니다. ($ = jQuery) https://learn.jquery.com/using-jquery-core/document-ready/

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

문서 로드가 완료될 때 실행됩니다.
M
MrNothing
7년 전
그렇군요. 제가 워드프레스로 제작하다보니, $ 대신 jQuery를 직접넣어야 먹히더라구. 참고하겠습니다. 감사합니다.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인