本文实例讲述了jQuery判断浏览器并动态调整select宽度的方法。分享给大家供大家参考,具体如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<mce:script src="jquery-1.4.4.js" mce_src="jquery-1.4.4.js" type="text/javascript"></mce:script>
<mce:script type="text/javascript"><!--
function userBrowser() {
var browserName = navigator.userAgent.toLowerCase();
if (/msie/i.test(browserName) && !/opera/.test(browserName)) {
browserName="ie";
} else if (/firefox/i.test(browserName)) {
browserName = "firefox";
} else if (/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName)) {
browserName = "chrome";
} else if (/opera/i.test(browserName)) {
browserName = "opera";
} else if (/webkit/i.test(browserName) && !(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName))) {
browserName = "safari";
} else {
browserName = "unknow";
}
return browserName;
}
$(function() {
var browser = userBrowser();
if (browser == "ie") {
$("select").each(function() {
$(this).css("width", ($(this).width() + 10) + "px");
});
}
else if (browser == "firefox") {
$("select").each(function() {
$(this).css("width", ($(this).width() + 8) + "px");
});
}
else if (browser == "chrome") {
$("select").each(function() {
$(this).css("width", ($(this).width() + 6) + "px");
});
}
else if (browser == "safari") {
$("select").each(function() {
$(this).css("width", ($(this).width() + 30) + "px");
});
}
});
// --></mce:script>
</head>
<body>
<div>
<!-- 注: select 在doctype下, 会出现width比同width的text短, ie为6px, ff为4px -->
<input id="t1" type="text" style="width: 400px;" /><br />
<select id="s1" style="width: 400px;">
<option>1</option>
</select><br />
<textarea id="TextArea1" cols="20" rows="2" style="width: 400px;">
</div>
</body>
</html>
希望本文所述对大家jQuery程序设计有所帮助。