>

스크롤 효과와 성공을 작성하려고했지만 더 잘 될 것이라고 생각하고 이러한 코드를 단순화하기 위해 조언이 필요합니다.

var nowIndex = 0;
var pcMenuSelect = $('body').find('.carFormatMenu li');
var mbMenuSelect = $('body').find('.menu-cartype .item');
var dropText = mbMenuSelect.parents('.dropdown').find('.text');
var dropValue = mbMenuSelect.parents('.dropdown').find('input')
if (nowIndex == 0){
    dropText.text(mbMenuSelect.eq(0).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(0).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(0).addClass('active selected');
    dropValue.eq(0).val(mbMenuSelect.eq(0).text());
}
if (nowIndex == 1){
    dropText.text(mbMenuSelect.eq(1).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(1).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(1).addClass('active selected');
    dropValue.eq(1).val(mbMenuSelect.eq(1).text());
}
if (nowIndex == 2){
    dropText.text(mbMenuSelect.eq(2).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(2).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(2).addClass('active selected');
    dropValue.eq(2).val(mbMenuSelect.eq(2).text());
}
if (nowIndex == 3){
    dropText.text(mbMenuSelect.eq(3).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(3).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(3).addClass('active selected');
    dropValue.eq(3).val(mbMenuSelect.eq(3).text());
}
if (nowIndex == 4){
    dropText.text(mbMenuSelect.eq(4).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(4).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(4).addClass('active selected');
    dropValue.eq(4).val(mbMenuSelect.eq(4).text());
}
if (nowIndex == 5){
    dropText.text(mbMenuSelect.eq(5).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(5).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(5).addClass('active selected');
    dropValue.eq(5).val(mbMenuSelect.eq(5).text());
}
if (nowIndex == 6){
    dropText.text(mbMenuSelect.eq(6).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(6).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(6).addClass('active selected');
    dropValue.eq(6).val(mbMenuSelect.eq(6).text());
}
if (nowIndex == 7){
    dropText.text(mbMenuSelect.eq(7).text());
    pcMenuSelect.removeClass('current');
    pcMenuSelect.eq(7).addClass('current');
    mbMenuSelect.removeClass('active selected');
    mbMenuSelect.eq(7).addClass('active selected');
    dropValue.eq(7).val(mbMenuSelect.eq(7).text());
}


  • 답변 # 1

    LalithKumar의 답변 사용 체인에 추가.

    var nowIndex = 0;
        var pcMenuSelect = $('body').find('.carFormatMenu li');
        var mbMenuSelect = $('body').find('.menu-cartype .item');
        var dropText = mbMenuSelect.parents('.dropdown').find('.text');
        var dropValue = mbMenuSelect.parents('.dropdown').find('input');
    if (nowIndex>0){
        dropText.text(mbMenuSelect.eq(nowIndex).text());
        pcMenuSelect.removeClass('current').eq(nowIndex).addClass('current');
        mbMenuSelect.removeClass('active selected').eq(nowIndex).addClass('active selected');
        dropValue.eq(nowIndex).val(mbMenuSelect.eq(nowIndex).text());
    }
    
    

  • 답변 # 2

    다음과 같이하십시오 :

    var nowIndex = 0;
        var pcMenuSelect = $('body').find('.carFormatMenu li');
        var mbMenuSelect = $('body').find('.menu-cartype .item');
        var dropText = mbMenuSelect.parents('.dropdown').find('.text');
        var dropValue = mbMenuSelect.parents('.dropdown').find('input');
    if (nowIndex>0){
        dropText.text(mbMenuSelect.eq(nowIndex).text());
        pcMenuSelect.removeClass('current');
        pcMenuSelect.eq(nowIndex).addClass('current');
        mbMenuSelect.removeClass('active selected');
        mbMenuSelect.eq(nowIndex).addClass('active selected');
        dropValue.eq(nowIndex).val(mbMenuSelect.eq(nowIndex).text());
    }
    
    

  • 답변 # 3

    Dinesh의 답변에 추가 :

    var nowIndex = 0;
        var pcMenuSelect = $('body').find('.carFormatMenu li');
        var mbMenuSelect = $('body').find('.menu-cartype .item');
        var dropMenu=mbMenuSelect.parents('.dropdown'); 
        if (nowIndex> 0){
            dropMenu.find('.text').text(mbMenuSelect.eq(nowIndex).text());
            pcMenuSelect.removeClass('current').eq(nowIndex).addClass('current');
            mbMenuSelect.removeClass('active selected').eq(nowIndex).addClass('active selected');
            dropMenu.find('input').eq(nowIndex).val(mbMenuSelect.eq(nowIndex).text());
        }
    
    

    mbMenuSelect의 캐싱을 완료했습니다. 그러나$( 'body')은 아닙니다. 찾기 기능을 다시 실행하면 실행 속도가 느려지기 때문입니다.

  • 답변 # 4

    공통 기능을 캡슐화하는 기능을 만드십시오.

    var pcMenuSelect = $('body').find('.carFormatMenu li')
    var mbMenuSelect = $('body').find('.menu-cartype .item')
    var dropText = mbMenuSelect.parents('.dropdown').find('.text')
    var dropValue = mbMenuSelect.parents('.dropdown').find('input')
    function updateSelect(index) {
      dropText.text(mbMenuSelect.eq(index).text())
      pcMenuSelect
        .removeClass('current')
        .eq(index)
        .addClass('current')
      mbMenuSelect
        .removeClass('active selected')
        .eq(index)
        .addClass('active selected')
      dropValue.eq(index).val(mbMenuSelect.eq(index).text())
    }
    // call the function with the index
    updateSelect(nowIndex)
    
    

  • 이전 asp.net - SignalR, Owin 및 예외 처리
  • 다음 java - 배포 후 언어 변경 - 기계적 인조 인간