JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

15.9 HTML Forms

In modern browsers, this getElementPos() method returns the same position values as getBoundingClientRect() does (but is much less efficient). Theoretically, a function such as getElementPos() could be used in browsers that do not support getBoundingClientRect(). In practice, however, browsers that do not support getBoundingClientRect() have a lot of element positioning incompatibilities and a function as simple as this one will not work reliably. Practical client-side libraries like jQuery include functions for computing element position that augment this basic position computation algorithm with a number of browser-specific bug fixes. If you need to compute element position and need your code to work in browsers that do not support getBoundingClientRect(), you should probably use a library like jQuery.

15.9 HTML Forms

The HTMLelement, and the various form input elements, such as,, and, have an important place in client-side programming. These HTML elements date from the very beginning of the Web and predate JavaScript itself. HTML forms are the mechanism behind the first generation of web applications, which required no JavaScript at all. User input is gathered in form elements; form submission sends that input to the server; the server processes the input and generates a new HTML page (usually with new form elements) for display by the client.

HTML form elements are still a great way to gather input from the user, even when form data is processed entirely by client-side JavaScript and never submitted to the server. With server-side programs, a form isn’t useful unless it has a Submit button. In client-side programming, on the other hand, a Submit button is never necessary (though it may still be useful). Server-side programs are based on form submissions—they process data in form-sized chunks—and this limits their interactivity. Client-side programs are event based—they can respond to events on individual form elements—and this allows them to be much more responsive. A client-side program might validate the user’s input as she types it, for example. Or it might respond to a click on a checkbox by enabling a set of options that are only meaningful when that box is checked.

The subsections that follow explain how to do these kinds of things with HTML forms. Forms are composed of HTML elements, just like any other part of an HTML document, and you can manipulate them with the DOM techniques already explained in this chapter. But form elements were the first ones to be made scriptable, in the earliest days of client-side programming, and they also support some APIs that predate the DOM.

Note that this section is about scripting HTML forms, not about the HTML itself. It assumes that you are already somewhat familiar with the HTML elements (,,, and so on) used to define those forms. Nevertheless, Ta ble 15-1 is a quick reference to the most commonly used form elements. You can read more about the form and form element APIs in Part IV , under the entries Form , Input , Option , Select , and TextArea .

Table 15-1. HTML form elements

HTML element Type property Event handler Description and events

or “button” onclick A push button

“checkbox” onchange A toggle button without radio button behavior

“file” onchange An input field for entering the name of a file to upload to the web server; valueproperty is read-only

“hidden” none Data submitted with the form but not visible to the user

none none A single item within a Select object; event handlers are on the Select object, not on individual Option objects

“password” onchange An input field for password entry—typed characters are not visible

15.9 HTML Forms | 397

HTML element Type property Event handler Description and events
“radio” onchange A toggle button with radio button
behavior—only one selected at a time
or “reset” onclick A push button that resets a form
“select-one” onchange A list or drop-down menu from which one
item may be selected (also see)
“select-multiple” onchange A list from which multiple items may be
selected (also see)
or “submit” onclick A push button that submits a form
“text” onchange A single-line text entry field; the default
element it typeattribute is
omitted or unrecognized
“textarea” onchange A multiline text entry field
友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1