jquery Operation Dom

Source: Internet
Author: User

1. Adding elements

<code>

<script language= "JavaScript" >
$ (). Ready (function () {
$ ("input[type= ' button ']"). Click (function () {
var $li = $ ("<li/>"). attr ("id", "sh"). attr ("name", "shanghai"). text ("shanghai");
$ ("#city"). Append ($li);//append the newly created element to the end of the child element of the specified element
});
});
</script>

</code>

<code>

2. deleting elements

<script language= "JavaScript" >
$ (). Ready (function () {
$ ("input[type= ' button ']"). Click (function () {
$ ("#cq"). Remove ();
});
});
</script>

</code>

3. Replacing elements

<code>

<script language= "JavaScript" >
$ ("input[type= ' button ']"). Click (function () {
$ ("button"). replacewith ($ ("p"));
});
</script>

</code>

4. Turn DOM objects into jquery objects

<code>

<script type= "text/javascript" >
Window.onload = function () {
Convert DOM objects to jquery objects
var domobj = document.getElementById ("username");//dom Object
var jqueryobj = $ (domobj); The object is a jquery object
Alert (jqueryobj.attr ("value"));
}
</script>

</code>

5, Select all, all do not select, and reverse election

<code>

<script type= "application/javascript" >
$ (). Ready (function () {
$ ("#checkItems"). Click (function () {
If ($ (this). attr ("checked")) {//selected
$ (": checkbox[name= ' items ']"). attr ("checked", true);
}else{
$ (": checkbox[name= ' items ']"). attr ("checked", false);
}
});

$ ("#checkall"). Click (function () {
$ (": checkbox[name= ' items ']"). attr ("checked", true);
$ ("#checkItems"). attr ("checked", true);
});

$ ("#checkallNo"). Click (function () {
$ (": checkbox[name= ' items ']"). attr ("checked", false);
$ ("#checkItems"). attr ("checked", false);
});

$ ("#check_revsern"). Click (function () {
$ (": checkbox[name= ' items ']:checked"). attr ("checked", false);
$ (": checkbox[name= ' items ']:not (: checked)"). attr ("checked", true);
$ (": checkbox[name= ' items ']"). each (function () {
If ($ (this). attr ("checked")) {//if selected
$ (this). attr ("checked", false);
}else{//not been selected
$ (this). attr ("checked", true);
}
});
});
});
</script>
<body>
You have a wide range of hobbies!!!
<br>
<input type= "checkbox" name= "checkitems" id= "checkitems" value= "select all/select all"/> Select All/select All
<br>
<input type= "checkbox" name= "items" value= "soccer"/> Soccer
<input type= "checkbox" name= "items" value= "basketball"/> Basketball
<input type= "checkbox" name= "items" value= "swimming"/> Swimming
<input type= "checkbox" name= "items" value= "singing"/> singing
<br>
<input type= "button" name= "checkall" id= "checkall" value= "select all"/>
<input type= "button" name= "checkall" id= "checkallno" value= "all not selected"/>
<input type= "button" name= "checkall" id= "check_revsern" value= "reverse selection"/>
</body>

</code>

6, jquery to achieve the choice of conversion

<code>

<script type= "text/javascript" >
$ (). Ready (function () {

$ ("#add"). Click (function () {
$ ("#second"). Append ($ ("#first option:selected"));
});

$ ("#add_all"). Click (function () {
$ ("#second"). Append ($ ("#first option"));
});

$ ("#remove"). Click (function () {

});

$ ("#remove_all"). Click (function () {

});
});
</script>
<body>

<div style= "border:1px dashed #E6E6E6; margin:150px 0px 0px 450px; width:350px; height:200px; ">
<table width= "285" height= "169" border= "0" align= "left" cellpadding= "0" cellspacing= "0" style= "margin:15px 0px 0px 15px; " >
<tr>
&LT;TD width= "126" >
<!--multiple= "multiple" can select multiple size= "10" to determine the length of the Drop-down selection--
<select name= "first" size= "ten" multiple= "multiple" class= "td3" id= "first" >
<option value= "option 1" > Options 1</option>
<option value= "option 2" > Options 2</option>
<option value= "option 3" > Options 3</option>
<option value= "option 4" > Options 4</option>
<option value= "option 5" > Options 5</option>
<option value= "option 6" > Options 6</option>
<option value= "option 7" > Options 7</option>
<option value= "option 8" > Options 8</option>
</select>
</td>
&LT;TD width= "valign=" "middle" >
<input name= "add" id= "add" type= "button" class= "button" value= "--"/>
<input name= "add_all" id= "add_all" type= "button" class= "button" value= "==>"/>
<input name= "remove" id= "remove" type= "button" class= "button" value= "&lt;--"/>
<input name= "remove_all" id= "remove_all" type= "button" class= "button" value= "&lt;=="/>
</td>
&LT;TD width= "127" align= "left" >
<select name= "second" size= "ten" multiple= "multiple" class= "td3" id= "second" >
<option value= "option 9" > Options 9</option>
</select>
</td>
</tr>
</table>
</div>
</body>

</code>

7, jquery to achieve edge change, edge increase

<code>

$ (). Ready (function () {
var index = 1;
/**
* Complete Add function
*/
$ ("#addUser"). Click (function () {
/**
* 1, get the name, phone, email value
* 2, Create
* <tr>
* <td><input type= "checkbox" ></td>
* <td>${name}</td>
* <td>${email}</td>
* <td>${phone}</td>
* <td><a> Edit </a></td>
* <td><a> Delete </a></td>
* </tr>
* 3. Add the completed TR to the Tbody
*/
var name = $ ("#name"). val ();
var email = $ ("#email"). val ();
var Tel = $ ("#tel"). val ();

var $checkbox = $ ("<input/>"). attr ("type", "checkbox");
var $checkboxTD = $ ("<td/>"). Append ($checkbox);
$checkboxTD. attr ("id", index);
index++;

var $nameTD = $ ("<td/>"). Text (name);

var $emailTD = $ ("<td/>"). Text (email);

var $phoneTD = $ ("<td/>"). Text (tel);

var $updateA = $ ("<a/>"). Text ("modify");
/**
* Function to complete the modification
*/
$updateA. CSS ("cursor", "pointer");
/**
* Add a click event to a modified hyperlink
*/
$updateA. Click (function () {
var name = $ (this). Parent (). siblings ("td:eq (1)"). text ();
var email = $ (this). Parent (). siblings ("td:eq (2)"). text ();
var Tel = $ (this). Parent (). siblings ("td:eq (3)"). text ();
var id = $ (this). parent (). siblings ("td:eq (0)"). attr ("id");

$ ("#name_update"). Val (name);
$ ("#email_update"). Val (email);
$ ("#tel_update"). Val (tel);
$ ("#id_update"). Val (id);
});
var $updateTD = $ ("<td/>"). Append ($updateA);

var $delA = $ ("<a/>"). Text ("delete");
$delA. CSS ("cursor", "pointer");

$delA. Click (function () {//finish Deleting the Bank's functions
If (window.confirm ("are you sure you want to delete It?")) {
$ (this). Parent (). Parent (). Remove ();
}
});

var $delTD = $ ("<td/>"). Append ($delA);

var $tr = $ ("<tr/>"). Append ($checkboxTD).
Append ($nameTD). Append ($emailTD).
Append ($phoneTD)
. Append ($delTD). Append ($updateTD);

$ ("#usertable tbody"). Append ($tr);
});

/**
* Complete deletion of several lines of functionality
*/
$ ("#deleteUser"). Click (function () {
If (window.confirm ("are you sure you want to delete It?")) {
/**
*: checkbox for all checkboxes
*: not (#allCheckbox) does not contain ID Allcheckbox
* The selected
*/
$ (": checkbox:not (#allCheckbox): checked"). Parent (). parent (). Remove ();
}
});

/**
* Full Select function
*/
$ ("#allCheckbox"). Click (function () {
If ($ (this). attr ("checked")) {
$ (": checkbox:not (#allCheckbox)"). attr ("checked", true);
}else{
$ (": checkbox:not (#allCheckbox)"). attr ("checked", false);
}
});

/**
* Complete the modification operation
*/
$ ("#updateUser"). Click (function () {
/**
* Gets the value of the modified ID to match the row in the table according to the value
*/
var idvalue = $ ("#id_update"). val ();
var name_update = $ ("#name_update"). val ();
var email_update = $ ("#email_update"). val ();
var tel_update = $ ("#tel_update"). val ();
/**
* Position a row (td) based on the value of the modified ID
*/
$ ("td[id=" +idvalue+ "]"). siblings ("td:eq (0)"). text (name_update);
$ ("td[id=" +idvalue+ "]"). siblings ("td:eq (1)"). text (email_update);
$ ("td[id=" +idvalue+ "]"). siblings ("td:eq (2)"). text (tel_update);
});
});

</code>

HTML code:

<code>

<script src= "user.js" ></script>
<body>
<center>
<br><br>
Add user:<br><br>
Name: <input type= "text" name= "name" id= "name"/>&nbsp;&nbsp;
Email: <input type= "text" name= "email" id= "email"/>&nbsp;&nbsp;
Tel: <input type= "text" name= "tel" id= "tel"/><br><br>
<button id= "addUser" > Submit </button>
<button id= "deleteuser" > Delete </button>
<br><br>
<br><br>
<table id= "usertable" border= "1" cellpadding= "5" cellspacing=0>
<tbody>
<tr>
<th><input type= "checkbox" id= "allcheckbox"/></th>
<th> name </th>
<th>email</th>
<th> Telephone </th>
<th> Delete </th>
<th> Modify </th>
</tr>
</tbody>
</table>
<br>
<br>
<br>
Modify User:<br><br>
Name: <input type= "text" name= "name" id= "name_update"/>&nbsp;&nbsp;
Email: <input type= "text" name= "email" id= "email_update"/>&nbsp;&nbsp;
Tel: <input type= "text" name= "tel" id= "tel_update"/><br><br>
<input type= "hidden" id= "id_update"/>
<button id= "updateUser" > Submit </button>
</center>
</body>

</code>

8. jquery Operation Dom

<code>

<script type= "text/javascript" >
Window.onload = function () {
/**
* jquery object is an array object
* jquery object cannot be null, if it is not worth, then the size of the array is 0
*/
var domobj = document.getElementById ("username");//dom Object
var jqueryobj = $ (domobj); The object is a jquery object
var domObj2 = Jqueryobj[0];//dom Object
var domObj3 = Jqueryobj.get (0);//dom Object
Alert (domobj2.getattribute ("name"));
Alert (domobj3.getattribute ("name"));
}
</script>
<body>
<input type= "text" name= "username" id= "username" >
</body>

</code>

jquery Operation Dom

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.