Use JavaScript to replace images randomly. use javascript to replace images.
This example describes how to use JavaScript to replace images randomly. Share it with you for your reference. The details are as follows:
The following code randomly replaces an image on the page with another image, or randomly displays an image.
The image file name is 0.gif 1.gif
Create a database tutorial
CREATE database cnnet;
Creating data Tables
CREATE TABLE ' cnnet '. ' Test ' (' ID ' INT (4) Not NULL auto_increment,' URL ' VARCHAR NULL,PRIMARY KEY (' id ')) ENGINE = MYISAM
Insert a few data:
INSERT into ' test ' (' id ', ' url ') VALUES(1, ' www.1aa.cm '),(2, ' www.1aa.cm '),(3, ' www.1aa.cm '),(4, ' www.1aa.cm '),(5, ' www.111cn.net '),(6, ' www.111cn.net ');
Now let's take a look at the introductory tutorial.
Creating a database conne
"keyword", the shortest data that matches the keyword is deleted $ {variable ## Keyword}. if the variable content starts with the "keyword ", the longest matching data will be deleted $ {variable % keyword}. if the variable content is forward from the end of the data with the "keyword ", delete the matching shortest data $ {variable % keyword}. if the variable content is forward from the end ", delete the longest matching data $ {variable/old string/new string}. if the variable content conforms
Method 1 (recommended ):Update tablename set fielda =Replace (cast (fielda as varchar (8000), 'A', 'bb ')Such a statement.Replace ntext in SQL. Instructions for using the text field:Replace (cast (fielda as varchar (8000), 'A', 'bb ')
Method 2:Only text fields are supported: The following functions and statements can be used with ntext, text, or image data.Function statementDatalength readtextPatindex set textsizeSubstring updatetextTextptr writetext
The conditional logic in the function makes it difficult for people to see the normal execution path.Use the Wei statement to show all special cases.
Motivation: conditional expressions are usually expressed in two forms. 1. All branches are normal. Second, only one of the answers provided by the conditional expressions is normal, and others are uncommon.
These two types of conditional expressions have different purposes. If the two branches are normal, you should use the form such as if ...
Open a text editor, press Ctrl + F to display the left-side pattern dialog box-Search dialog box, press Ctrl + H to display the right-side pattern dialog box-replace dialog box, two struct and two functions are defined in windows, so that we can easily create these two dialogs during programming.
Unlike other dialogs in the system, these two dialogs are non-modal, allowing users to switch back and forth between the main window and the two windows.
B
Replace newline and replace
Example: str = str.replace (/\r\n/g, "
str = str.replace (/
Another point:
/n or/r/n or/R may be line breaks, depends on whether your system is windows or Unix/linux or MacAlternatively,/s+ can be replaced under Windows, but/s+ will also match other characters, not a safe approach.
The cloud-dwelling community supplements:
In practical applications, you can not guarantee t
Use Web Effects str.replace (/-/g, "!") You can replace all the matching characters (G is the global flag).
String.Replace (/reallydo/g, replacewith);String.Replace (New RegExp (Reallydo, ' G '), replacewith);String: An expression containing a substring to override.Reallydo: the substring being searched.ReplaceWith: a substring to replace.
1, JS string replacement and replace () method of Use
The Replace (Regexp,replacement) method has two parameters, the first parameter can be a plain text string or a RegExp object, see the use of RegExp objects, and the second argument is a string or a function.
Here are some examples of JS string replacements:
Example 1:
Copy Code code as follows:
var str= "Hello world!";
document.write
Values char (' ABCD ', ' A ', ' OPMN ') returns the result of OPMNBCDUpdate EMP Set Name=replace (name, ' D ', ' W ') where empno=70The key to the use of this function is case-sensitive, there can be no space between the strings, otherwise it cannot be replaced.Update test111 set A=replace[object Object] (A,CHR (13) | | Chr (10), ' 1 ') This is a substitution statement, Chr (13) | | CHR (10) wraps the carri
One, string lookup 1. Using/And in Vim to find a string, the difference is that /string highlights the first string that matches the cursor after the carriage return, and the cursor moves to the first letter of the string; ? string highlights the first match before the cursor A string that returns the first letter of the string after the cursor moves to it. After carriage return, press N to go to the next matching string in the same direction, and press N to go in the opposite direction to the
Grammar
REPLACE (' String_expression1 ', ' string_expression2 ', ' String_expression3 ')
Parameter description
' String_expression1 '
The string expression to search for. String_expression1 can be either character data or binary data.
' String_expression2 '
The string expression to find. String_expression2 can be either character data or binary data.
' String_expression3 '
The string expression to replace.
Common effects on Weibo or forums: when inserting an emoticon into a text domain, a string similar to [Haha] is displayed. Below is a method to convert an emoticon string to an image emoticon.Copy codeThe Code is as follows: Copy codeThe Code is as follows:Var face = {'[Haha]': '', '[sweating]': ''} // For the complete QQ expression, see the end of this ArticleVar reg =/\ [. +? \]/G;Var str = '[Haha] abc [sweating] [sweating]'; // here is the value of the obtained text field. For simplicity, str
When you select an emoticon to insert a text field, a string similar to [Haha] is displayed. Below is a method to convert an emoticon string to an image emoticon, which is common in Weibo or forum, when you select an emoticon to insert a text field, a string similar to [Haha] is displayed. Below is a method to convert an emoticon string to an image emoticon.
The Code is as follows:
Abc
The Code is as follows:
Var face = {'[Haha]': '', '[sweating]':''} // For the complete QQ exp
You have a large function where the use of local variables makes it impossible to use the extract method (extraction function ).Put this large function into a single object, so that local variables become fields in the object. Then you can break down this large function into multiple small functions in the same object.
Motivation: the presence of local variables increases the difficulty of function decomposition. If local variables in a function are flooded, it is very difficult to break down
Replaces the URL in a paragraph of text entered by a user with a link address that can be clicked. For example: Http://www.111cn.net can replace [Url]http://www.cctv.com[/url]
or
The key to this is to match the link, after matching, add a tag and attributes on both sides is not a problem.
/http://[w-]* (. [ w-]*) +/ig first match http://.
[w-]* is possible www and BBS etc.
. [w-]* match. XXX form, at least one.
The test code is as follows:
Copy Code code as follows:
function fn ()
{
for (var i = 0;i Alert ("First" + (i+1) + "value of parameter:" +arguments[i]);
}
}
var str = ' Str.replace (/\{([a-z]+) \}/ig, FN);
Based on multiple tests, the output results can be drawn from the FN:
The first parameter is a matching string, such as {wo} and {ni};
The second argument can have 0-n, matching the first argument to a regular string of parentheses, such as Wo and NI in the first argument, to match ([a-z]+),
There a
This example describes a JavaScript regular expression that replaces the phone number with replace (). Share to everyone for your reference.
The implementation methods are as follows:
Copy Code code as follows:
I hope this article will help you with your JavaScript programming.
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.