Favorites: A small set of JavaScript tips and features

Source: Internet
Author: User
Javascript| Tips

Inserts the text that contains the cursor

<input onblur= "Global=this" ><br/>
<input onblur= "Global=this" ><br/>
<input Type=button Value=meizz >
<script language= "JavaScript" >
var global = null; Which text box is the record to insert
function mm ()
{
var old = Window.clipboardData.getData ("Text");
var txt = window.prompt ("Please enter", "");
if (TXT && global)
{
Window.clipboardData.setData ("Text", txt);
Global.focus ();
Document.execcommand (' Paste ');
Global=null;
}
Window.clipboardData.setData ("Text", old);
}
</SCRIPT>

Press ENTER to submit

<script language= "JavaScript" >
<!--
Document.onkeydown=function ()
{
Loginform.submit ()
}
-->
</script>

Change the status of TD

<script>
function Click_b () {
div1.innerhtml = "<input type=text id=t1>";
Div1.innertext = "Test";
}
function Click_b2 () {
div1.innerhtml = T1.value;
}
</script>
<table><tr><td><div id=div1> Click Before </div></td></tr></table>
<input Type=button value= "Change" >
<input Type=button value= "Save" >

Determine if check box is selected

<script language= "JavaScript" >
<!--
function Check ()
{
var O1 = document.getelementsbyname (' R ');
var O2 = Document.getelementsbyname (' C ');
var i;
var j=0,p=0;
for (i=0;i<o1.length;i++)
{
if (o1[i].checked)
{
Alert (' Selected radio button ID: ' +o1[i].id ');
j + +;
}
}
if (j==0) alert (' No radio button selected! ');
for (i=0;i<o2.length;i++)
{
if (o2[i].checked)
{
Alert (' Selected multiple-select button ID: ' +o2[i].id ');
p++;
}
}
if (p==0) alert (' No multiple selection buttons! ');
}
-->
</script>

<input Type=radio name=r ID=R1>R1
<input Type=radio name=r ID=R2>R2
<input Type=radio name=r ID=R3>R3
<br>
<input Type=checkbox name=c ID=C1>C1
<input Type=checkbox name=c ID=C2>C2
<input Type=checkbox name=c id=c3>c3
<input Type=checkbox name=c ID=C4>C4
<input Type=checkbox name=c Id=c5>c5
<p><input Type=button Value=check ></p>

Take the value of input

<script language= "JavaScript" >
<!--
function Check ()
{
var o = document.getelementsbytagname (' INPUT ');
for (Var i=0;i<o.length;i++)
{
if (O[i].type = = ' Radio ' && o[i].checked) alert (' Value of the selected checkbox: ' +o[i].value);
if (O[i].type = = ' checkbox ' && o[i].checked) alert (' Value of the selected check box: ' +o[i].value);
}
}
-->
</script>

Select All

<form name=meizz>
<input Type=checkbox Name=all. <br/>
<input Type=checkbox name=mm ><br/>
<input Type=checkbox name=mm ><br/>
<input Type=checkbox name=mm ><br/>
<input Type=checkbox name=mm ><br/>
<input Type=checkbox name=mm ><br/><br/>


<input Type=checkbox name=all2. <br/>
<input Type=checkbox name=mm2 ><br/>
<input Type=checkbox name=mm2 ><br/>
<input Type=checkbox name=mm2 ><br/>
<input Type=checkbox name=mm2 ><br/>
<input Type=checkbox name=mm2 ><br/>

</form>

<script language= "JavaScript"
function Checkall (str)
{
  var a = Document.getelementsbyname ( STR);
  var n = a.length;
  for (var i=0 i<n; i++)
  a[i].checked = window.event.srcElement.checked;
}
function Checkitem (str)
{
  var e = window.event.srcElement;
  var all = eval ("Document.meizz." + str);
  if (e.checked)
  {
    var a = Document.getelementsbyname (e.name);
    all.checked = true;
    for (var i=0 i<a.length; i++)
    {
      if (!a[i].checked) {all.checked = false; break;}
   }
 }
  Else all.checked = false;
}
</script>

<input Type=radio name=r value=1>
<input Type=radio name=r value=2>
<input Type=radio name=r value=3>
<p>
<input Type=checkbox name=c value=1>
<input Type=checkbox name=c value=2>
<input Type=checkbox name=c value=3>
<p>
<input Type=button Value=check >

How to get the number of TD lines

<table border=1>
<tr>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
</tr>
<tr>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
</tr>
<tr>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
&LT;TD onclick= ' alert (number of rows: + (this.parentelement.rowindex+1) + "\ n column Number:" + (this.cellindex+1)) ' > Dot i </td>
</tr>
</table>

---------------------------------------
According to TD, can be as follows:

OTd.parentNode.parentNode.parentNode.rows.length Number of rows
Number of oTd.parentNode.cells.length columns
------------------------------------------
OTd.parentNode.parentNode.rows.length Number of rows

How to make full-text substitutions

<script>
var strText = new String ();
strtext= "<input type=text><input type=submit>"
alert (StrText);
var re = new RegExp ("<", "GI");
var newstr = Strtext.replace (Re, "<");
alert (NEWSTR);
</script>

Time format

<script language=javascript>
Alert ("2004-9-3". Replace (/\-(\d{1})/ig, " -0$1")
</script>

<script language= "VbScript" >
Set re = new REGEXP
Re. Pattern = "\-(\d{1})"
Re. Global = True
MsgBox Re. Replace ("2004-9-3", " -0$1")
Set RE =nothing
</script>

The judgment of Chinese punctuation

<input Type=text name=a>
<input Type=button value= "Submit" >

<script language= "JavaScript" >

function Chk ()
{
B=/^\s*[a-za-z0-9\u4e00-\u9fa5,. ;:“”‘’!? ,]*\s*$/;
C=document.all.a.value;
if (C.replace (b, "")!= "")
{
Alert ("You entered an illegal character");
}

}
</script>



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.