Home > Web Front-end > JS Tutorial > body text

How to Solve Drop-Down List Width Difference Between Internet Explorer and Firefox Using jQuery?

Susan Sarandon
Release: 2024-10-20 15:03:29
Original
507 people have browsed it

How to Solve Drop-Down List Width Difference Between Internet Explorer and Firefox Using jQuery?

IE Dropdown List Width Discrepancy

In Internet Explorer, dropdown lists inherit the width of the dropbox, while in Firefox they adjust to the list content. This can be problematic, requiring dropboxes to be unnecessarily oversized to accommodate the longest option.

CSS Solution

Unfortunately, setting different widths for the dropbox and dropdown list is not possible using CSS alone.

jQuery Workaround

A workaround using jQuery can achieve the desired effect. This solution captures various events (focus, mouseover, click, mouseout, blur) and dynamically modifies the CSS class of the dropdown list.

if (!$.support.leadingWhitespace) { // if IE6/7/8
    $('select.wide')
        .bind('focus mouseover', function() { $(this).addClass('expand').removeClass('clicked'); })
        .bind('click', function() { $(this).toggleClass('clicked'); })
        .bind('mouseout', function() { if (!$(this).hasClass('clicked')) { $(this).removeClass('expand'); }})
        .bind('blur', function() { $(this).removeClass('expand clicked'); });
}
Copy after login

Additionally, the following CSS is required:

select {
    width: 150px; /* Or whatever width you want. */
}
select.expand {
    width: auto;
}
Copy after login

By assigning the "wide" class to the dropdown list, the workaround adjusts its width accordingly, solving the IE discrepancy.

The above is the detailed content of How to Solve Drop-Down List Width Difference Between Internet Explorer and Firefox Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!