简体   繁体   中英

Add “width=device-width” in meta name=“viewport” if mobile phone

need to add "width=device-width" in meta name="viewport" if mobile phone. trying to use this to no avail:

//iPhone Fix
jQuery(document).ready(function(){
    if (jQuery(window).width < 767)  {
        var meta = document.getElementsByTagName("meta");
        meta.setAttribute("width","device-width");
    }
}); 

If i dont set width="device-width" in the meta tag, site looks great on phones and media.css kicks in, but tablets become distorted. Any help would be great. Thanks

I think you have just a little error in your code, this should work:

//iPhone Fix
jQuery(document).ready(function(){
    if (jQuery(window).width < 767)  {
        var meta = document.getElementsByTagName("meta");
        meta[0].setAttribute("width","device-width");
    }
});

In your original code, you tried to use the setAttribute method on an array of elements and not an element itself. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM