Common jquery Code snippets and JavaScript code snippets

Source: Internet
Author: User
1. jquery Chapter


1. jquery Gets the value of the element named Price under #first

function Getit () {
       name = $ ("#first input[name= ' price ']"). Val ();
       alert (name);  
Alert ($ ("#first input[name= ' price ']"). attr ("value")); Gets the value of a property

$ ("ul Li:last"). HTML ("22"); $ ("ul Li:first"). HTML ("22"); 2. Val () can assign a value or get a value

Alert ($ ("#first input[name= ' price ']"). Val ());
$ ("#first input[name= ' price ']"). Val (123123123)

3. Each () traverses the option item in the Select

$ ("#sele option"). each (function () {
    if ($ (). Text () = = "Text") {
    $ (this). attr ("Selected", "true");
    }
  });

4. Addition of JavaScript

parseint (name) +parseint (name2)

5. Run when page loads

$ (document). Ready (function () {
            $ (' ul '). CSS (' Color ', ' red ');
 });


6. jquery Gets the value of the element method

$ ("#orderedlist"). Find ("Li") is equivalent to  $ ("#orderedlist li")
 
Var  nname=$ (this). Parents ('. List-comt '). Find (' #nnuser '). html ();   Find the value of a child element in the parent element of an element

7. Append after element

$ ("ul Li"). each (function () {
  $ (this). Append ("<font style= ' font-weight:bold; >fireworf</font> ");
 
$ (this). Find ("em"). Remove (); Deletes the element
//$ ("a[href= '/fyou ')"). html (); Gets the hyperlink for the specified link
//$ ("a[href]")//a hyperlink with an href attribute

8. Change the object's CSS

$ ("#61dh a"). css (' color ', ' #123456 ');
 
$ (' #sp '). Parent (). CSS (' background ', ' #000 ');  #sp的父原素的背景变黑

9. Clear Form form Reset

$ ("#reset"). Click (function ()
{
 $ ("#form") [0].reset ()
 }]


10. Binding Events

Functionsecondclick () {
    $ ("#dv1"). Unbind ();//Dismiss Object Event
    alert ("Second click!");
}
$ ("#dv1"). Bind ("MouseOver", Secondclick);
 
$ ("#dv1"). Trigger ("click");  Automatically triggers event
//Adds a Click event for all paragraphs, including the element
    $ ("P") that is subsequently added. Live ("Click", Function () {
        alert ($ (this). html ())
;


11. Animation Effect Custom Animate

$ ("#go"). Click (function () {
 
$ ("#block"). Animate ({height:300,width: ' +800 ', marginleft: ' +=300 '}, "slow");
});
 
+800 indicates that the increase to      +=800 indicated on the original basis of a
//$ (". Panel"). Slidetoggle ("slow");  Up and down synthesis


JavaScript Native binding events

var dg = document.getElementById ("DG");
Dg.onmouseover =function () {
  
   alert (' good ');
  
   }

A . Return false is out of this method in JavaScript.   jquery added Delete class

$ (thisobj). AddClass ("selected");
$ (thisobj). Parents ("ul"). Find ("Li"). Removeclass ("selected");

15. Select Value

var pp = $ ("#provide option:selected"). Val ();
or
var area0 = $ ("select[name= ' prov ')"). Val ();

16. Insert content in an element (can be HTML, can be a string)

Insert content at the end of each P element:

$ ("button"). Click (function () {
 $ ("P"). Append ("<b>Helloworld!</b>");
});


Slidetoggle and Slidedown, Slideup

$ (". Clickdiv"). Click (function () {  
           $ (this). Slidetoggle;  By using the slide effect, toggle between the display and the hidden state of the <p> element Slidedown and Slideup
           $ (this). Slidedown ();  Show block Element

        });  




2 JavaScript posts
1. Find HTML elements by ID

var X=document.getelementbyid ("Intro");

This example looks for the element of id= "main" and then looks for all the <p> elements in "main":

var X=document.getelementbyid ("main");
var y= x.getelementsbytagname ("P");
var xx = document. getelementsbyname (' name ');

2. This example changes the attributes of an element


document.getElementById ("image"). src= "Landscape.jpg";


The 3.javascript event assigns the OnClick event to the button element:

<button id= "Mybtn" > Press me </button>
<script>
document.getElementById ("mybtn"). onclick= function () {displaydate ()};
</script>

4. Change CSS

<p id= "P2" >hello world!</p>
 
<script>
document.getElementById ("P2"). style.color= "Blue" ;
</script>

5. Change the contents of the inside
<p id= "P2" >hello world!</p>
 
<script> document.getElementById ("P2"). InnerHTML = " 12333333333333 ";
</script>

6. Connection string
function ff () {
 alert ("abc" + "DfE");
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.