jQuery in Action

Using the jQuery flags
159
Because there's no way to perform object detection to determine if we should
pass an object reference or an integer value, we must resort to browser detection
as shown in the example page of listing 6.1, which can be found in the file
chapter6/$.browser.html.
<html>
<head>
<title>$.browser Example</title>
<link rel="stylesheet" type="text/css" href="../common.css">
<script type="text/javascript"
src="../scripts/jquery-1.2.1.js"></script>
<script type="text/javascript">
$(function(){
$('#testButton').click(function(event){
var select = $('#testSubject')[0];
select.add(
new Option('Two and \u00BD','2.5'),
$.browser.msie ? 2 : select.options
);
});
});
</script>
</head>
<body class="plain">
<select id="testSubject" multiple="multiple" size="5">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
<div>
<button type="button" id="testButton">Click me!</button>
</div>
</body>
</html>
This example sets up a
<select>
element with four entries and a simple button.
The button is instrumented to add a new
<option>
element between the second
and third original options. Because Internet Explorer will expect the ordinal
value
2
, but
W3C
-standard browsers will expect a reference to the third existing
option, we use browser detection to branch what gets passed as the second
parameter to the
add()
method.
The before-and-after results for a sampling of browsers are shown in figure 6.1.
Listing 6.1 Testing for browsers
Employs browser
detection for second
parameter


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.