我有一个名为android.html的页面,带有一个ID为“ detect”的按钮。当您单击按钮时,脚本会通过us标头检查您的android设备上的操作系统版本。如果版本3.0或更高版本,我如何更改脚本以应用样式;如果版本2.9或更低版本,我如何更改脚本以应用样式。这是困难的部分...我不想将css样式应用于android.html页面,而是应用于单击按钮将重定向到的playlist.html页面。

html

<div id="detect">button</div>


js

$('#detect').click(function() {
if (/Android (\d+(?:\.\d+)+)/.test(navigator.userAgent)){
 var version=new Number(RegExp.$1)
 if (version>=3)
  //redirect to playlist.html using style1.css
 }
else
 //redirect to playlist.html using style2.css
});

最佳答案

可以将查询字符串键/值对添加到重定向中,以使下一页知道要加载的样式表,或者仅在下一页加载时检查用户代理字符串。

$('#detect').click(function() {
    if (/Android (\d+(?:\.\d+)+)/.test(navigator.userAgent)){
        var version=new Number(RegExp.$1)
        if (version>=3)
            window.location = 'playlist.html?style=1';
        } else {
            window.location = 'playlist.html?style=2';
        }
    }
});


然后在playlist.html页面上:

if (window.locaion.search.indexOf('style=1') > -1) {
    $.getScript('style1.css');
} else if (window.locaion.search.indexOf('style=2') > -1) {
    $.getScript('style2.css');
}


if/then检查非常简单。更好的方法是获取数组中的查询字符串参数,检查style键,然后加载相应的值。

关于javascript - jQuery的更改CSS的下一页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9203595/

10-14 13:12
查看更多