The basic function of jQuery (), -queries - Designing...

The base function of jQuery ()

The workaround and transformation of the document's object model usually begins with the search for the required elements. In normal JavaScript, for example, the functions getEI-ementsByTagName or getElementByld are used to select an item. In jQuery, for this purpose, the jQuery () function (short entry $ ()), whose syntax is a combination of CSS and regular expressions:

jQuery (expression, [context]) or $ (expression, [context])

This function takes as the first parameter the expression string containing the search expression (CSS selector). The second parameter to context (optional) is DOM elements, document objects, or jQuery objects that specify the search scope (search context). The jQuery () function returns a special JavaScript object that contains an array of DOM-model elements that match the query (selector) specified in the parameter.

For example, to get an element with the identifier #myElement, you need to write

jQuery ('' # myElement ") or $ (" # myElement ").

The jQuery object (set of elements) has a large number of methods that affect each element of this set. In particular, you can add event handlers to them. For example, to display a message after clicking a button, you can place JavaScript in the onclick event:

button id = myButton onclick = alert ('l was clicked!') & gt; Click me! & lt;/button & gt;

The drawback of this approach is that it mixes code with markup, which can complicate application support and its logic. Using jQuery, you can add an external event handler to push a button.

button id = myButton & gt; Click me! & lt;/button & gt;

script type = text/javascript "& gt;

$ ('button # myButton'). click (function () {alert ('l was clicked!');});

& lt;/script & gt;

Due to this separation of the markup of the site and its code (behavior), the site's support is facilitated, the code readability improves.

Similarly, you can add a handler for the ready event to an entire page. It will start after the hierarchy of DOM objects for the page is formed. Thus, it is best if events and other jQuery code are contained in the ready event handler:

$ (document) .ready (function () {

$ ('button # myButton'). click (function () {alert ('Button was clicked!');});

}):

The result is the same as in the previous example, but in a more secure way, in which the DOM is initially loaded, and only then an event handler is attached to the button.

queries

Using jQuery, it's very easy to create Ajax queries. For example, the following is an Ajax request that is executed in response to a button click (similar to the one discussed earlier in the "Ajax Technology" section): "script type =" text/javascript '' & gt;

$ (document) .ready (function () {$ ( button ''). click (function () {

$ ("# otvet ''). load (" request.txt );});

});

& lt;/script & gt;

Ajax-query handlers are very easy to write using PHP and ASP.Net ICC.

Also We Can Offer!

Ошибка в функции вывода объектов.