e-domen dot com Blog


Prototype cheat sheet

Posted in Доки, Javascript by maxim on the Октябрь 23rd, 2006
Есть такая замечательная либа в жабаскрипте - Prototype

Чит по ней взят здесь
Element Functions

$()
obj = $('elementID');
Get the html element with the ID of elementID
objArray = $("element1","element2");
Get an array containing multiple elements
document.getElementsByClassName("theclass);
Get an array containing all elements with the class theclass
Element.removeClassName(elementID,theclass);
Remove the class from the element.
Element.addClassName(elementID,theclass);
Add the class theclass to the element
new Insertion.Before(elementid,text here);
Inserts the text directly before the elementid element.
text hereOriginal
new Insertion.Top(elementid,text here);
Inserts the text inside the element at the top:
text here Original
new Insertion.Bottom(elementid, text here);
Inserts the text inside the element at the bottom:
Original text here
new Insertion.After(elementid,text here);
Inserts the text directly after the elementid element.
Originaltext here
$(element1,element2).each( function (theobj){
alert(theobj.innerHTML);
});
Traverse through the array using the .each syntax similar to ruby.
Element.hide(elementid);
Hides the element
Element.show(elementid);
Shows the element
Element.toggle(elementid);
Toggles the show/hide status of an element
Element.remove(elementid);
Remove an element from the page

Form Functions

$F(fieldname);
Return the value of the form element, whether it is a text input, textarea,
select box or checkbox. If it is a checkbox, it will return undefined if
unchecked. Radio groups dont work.
Form.getElements(formID);
Returns an array of all the form elements for form formID
Form.serialize(formID);
Returns a formatted URL containing all the elements in the form, similar to
&field=value&field2=othervalue
Form.focusFirstElement(formID);
Will set focus on the first form element.

Exception Handling

Try.these(
function(){
// errors
},
function(){
// other stuff
}
);
Allows you to execute the second function if the first one fails.
Kinda like try/catch, except it doesn’t make any sense.

Ajax

function ajaxMe( theUrl, data ){
var ajaxRequest = new Ajax.Request(
theUrl,{method: post, parameters: data, onComplete: theResponse});
}

function theResponse(origRequest){
alert(origRequest.responseText);
}

Leave a Reply

You must be logged in to post a comment.