"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > JavaScript로 호버링 가능한 선택 상자 옵션을 만드는 방법은 무엇입니까?

JavaScript로 호버링 가능한 선택 상자 옵션을 만드는 방법은 무엇입니까?

2024년 12월 21일에 게시됨
검색:818

How to Create Hoverable Select Box Options with JavaScript?

가져올 수 있는 선택 상자 옵션

당면한 질문은 클릭하여 여는 대신 필드 위로 마우스를 가져갈 때 옵션 설명이 표시되는 선택 상자를 만드는 것입니다. 옵션.

구현

이 기능을 구현하기 위해 우리는 JavaScript 접근 방식을 다음과 같이 활용했습니다. 다음:

$('#selectUl li:not(":first")').addClass('unselected');
// Hide the 'unselected' elements in the ul.

$('#selectUl').hover(
    function(){
    // mouse-over
        $(this).find('li').click(
            function(){
                $('.unselected').removeClass('unselected');
                // removes the 'unselected' style

                $(this).siblings('li').addClass('unselected');
                // adds 'unselected' style to all other li elements

                var index = $(this).index();
                $('select option:selected').removeAttr('selected');
                // deselects the previously-chosen option in the select

                $('select[name=size]')
                    .find('option:eq('   index   ')')
                    .attr('selected',true);
                // assumes a 1:1 relationship between the li and option elements
            });
    },
    function(){
    // mouseout (or mouseleave, if they're different, I can't remember).
    });

이 접근 방식에서는 선택되지 않은 클래스를 사용하여 처음에 첫 번째 옵션을 제외한 모든 옵션을 숨깁니다. ul 위에 마우스를 올리면 클릭하지 않은 요소에 선택되지 않은 클래스가 부여되어 사실상 사라집니다. 선택한 요소는 계속 표시되며 해당 값은 기본 선택 필드에 반영됩니다.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3