JS to dynamically modify the url to add, query, modify, and delete the url.

Source: Internet
Author: User

JS to dynamically modify the url to add, query, modify, and delete the url.

Although you can modify the url dynamically by submitting a post form using the get method, if multiple buttons can be submitted in parallel, write multiple forms with the same details and differences, it is inevitable that there is something wrong. Therefore, I thought of using JS to dynamically modify the url to add, query, modify, and delete the url.

<Script> var LG = (function (lg) {var objURL = function (url) {this. ourl = url | window. location. href; this. href = "";//? Previous section this. params = {}; // url parameter object this. jing = ""; // # And this. init ();} // analyze the url? Save this. href. The parameter is resolved to this. params object, # And save it to this. jingobjURL. prototype. init = function () {var str = this. ourl; var index = str. indexOf ("#"); if (index> 0) {this. jing = str. substr (index); str = str. substring (0, index);} index = str. indexOf ("? "); If (index> 0) {this. href = str. substring (0, index); str = str. substr (index + 1); var parts = str. split ("&"); for (var I = 0; I <parts. length; I ++) {var kv = parts [I]. split ("="); this. params [kv [0] = kv [1] ;}} else {this. href = this. ourl; this. params = {};}// only modify this. paramsobjURL. prototype. set = function (key, val) {this. params [key] = val;} // only sets this. paramsobjURL. prototype. remove = function (key) {this. params [key] = undefined;} // ur after the three-part Operation LobjURL. prototype. url = function () {var strurl = this. href; var objps = []; // The array is used here, And the join operation for (var k in this. params) {if (this. params [k]) {objps. push (k + "=" + this. params [k]) ;}} if (objps. length> 0) {strurl + = "? "+ Objps. join ("&");} if (this. jing. length> 0) {strurl + = this. jing;} return strurl;} // obtain the parameter value objURL. prototype. get = function (key) {return this. params [key];} lg. URL = objURL; return lg ;}( LG ||{}); var myurl = new LG. URL (window. location. href); myurl. remove ("B"); // The balert (myurl. get ("a"); // obtain the value of parameter a. Here, we get 1myurl. set ("a", 23); // modify the value of a to 23 alert (myurl. url (); </script>

JavaScript to implement dynamic URLs

Adding javascript scripts in href does not work. Dynamic URLs can be implemented only when a function is called by triggering an event. Below is an implementation method.
<Script language = "javascript">
Var tempUrl = "";
Function addText (obj ){
TempUrl = obj. href;
Obj. href + = obj. innerText;
}
Function delText (obj ){
Obj. href = tempUrl;
}
</Script>

<A href = "www.abc.com #" onmouseover = "addText (this)" onmouseout = "delText (this)">
Link text
</A>

Jquery is used for simple addition, deletion, modification, and query without interface. It is best to add, delete, modify, and Query files. Each file is written in a js file. The framework is not limited and the project is uploaded.

You can give you a static page operation. If you want to use jquery to operate the database, contact Bill Gates.
// Add data and write a button with the id addtable.
$ (Document). ready (function (){
Var len = 0;
$ ("# Addtable"). click (function (){
Len ++;
$ ("# Table1"). append ("write the td you want to add here, write it by yourself, I only write Delete" +
"<Td>" +
"<Input type = 'button 'id = 'td" + len + 1 + "5 'onclick = 'del (this); detailJudge () 'value = 'delete'/> "+
"</Td>" +
"</Tr> ")
})
})
// Delete
Function del (r ){
If (confirm ("are you sure you want to delete? ")){
Var I = r. parentNode. parentNode. rowIndex;
Document. getElementById ('table1'). deleteRow (I );
}
}
// Query:
Jsp: the page is written as follows:
Query: <input type = "text" id = "shai"/>
Js: Write as follows:
$ (Document). ready (function (){
$ ("# Shai"). keyup (function (){
$ ("Table tbody tr "). hide (). filter (": contains ('" + ($ (this ). val () + "')"). show ();

}); Keyup (); // This sentence must exist.
})
// You can understand it, where $ ("table tbody tr") is modified according to your situation. Here: You need to find the tr under the tbody tag under the table tag.

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.