I want to do position absolute for all other browser except IE in jquery. I have the following code.
<script type="text/javascript">
var positions=$("#gift_field").position();
var top=positions.top;
$("#discount_box").css("float","right");
if (!$.browser.msie){
$("#discount_box").css("position","absolute");
}
$(".discount_box").css("top",positions.top);
var wd=(positio开发者_如何转开发ns.left) + 400;
$(".discount_box").css("left",wd);
</script>
But this is not working.
2 things...
browser is deprecated - suggest you use feature detection instead.
absolute positioning is fine in IE (even 6) if you use a strict doctype - and any fixes can be done in IE specific style sheets included with conditional comments.
Are you sure that what you're trying to do is correct in every past, present and future version of IE? What problem do you want to work around? Maybe use something like Modernizr, add a special case in your CSS, and have it stop being used automatically as soon as IE acts like the rest of the browsers?
If you are absolutely sure that what you want is to detect IE8 (or any other version) then you don't need any JavaScript to do it – you can use something like this:
<!doctype html>
<!--[if lt IE 7 ]> <html lang="en-us" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en-us" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en-us" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en-us" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en-us" class="no-js"> <!--<![endif]-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" >
<meta charset="utf-8">
...
and then use CSS like this:
#discount_box { your normal style }
.ie8 #discount_box { additional style for IE8 }
(but check if discount_box
is a class or ID, because you use both in your example)
See: Conditional stylesheets vs CSS hacks? Answer: Neither! by Paul Irish.
to detect browser versions through jquery see Jquery Browser
if (typeof $.browser.msie == 'undefined') { ...
$.browser.msie is not supported in latest version of jquery you can either add jquery-migrate-1.2.1.min.js or use following jquery function... for IE it also gives you version ...
call currentBrowser().browser to detect browser .........
function currentBrowser() {
$.returnVal = "";
var browserUserAgent = navigator.userAgent;
if (browserUserAgent.indexOf("Firefox") > -1) {
$.returnVal = { browser: "Firefox" };
}
else if (browserUserAgent.indexOf("Chrome") > -1) {
$.returnVal = { browser: "Chrome" };
}
else if (browserUserAgent.indexOf("Safari") > -1) {
$.returnVal = { browser: "Safari" };
}
else if (browserUserAgent.indexOf("MSIE") > -1) {
var splitUserAgent = browserUserAgent.split(";");
for (var val in splitUserAgent) {
if (splitUserAgent[val].match("MSIE")) {
var IEVersion = parseInt(splitUserAgent[val].substr(5, splitUserAgent[val].length));
}
}
$.returnVal = { browser: "IE", version: IEVersion };
}
else if (browserUserAgent.indexOf("Opera") > -1) {
$.returnVal = { browser: "Opera" };
}
else {
$.returnVal =
{ browser: "other" };
}
return $.returnVal;
}
精彩评论