javascript|技巧
插入游標所在的Text
<input onblur="global=this"><br/>
<input onblur="global=this"><br/>
<input type=button value=meizz >
<SCRIPT LANGUAGE="JavaScript">
var global = null; //記錄是要在哪個文字框裡插入
function mm()
{
var old = window.clipboardData.getData("Text");
var txt = window.prompt("請輸入", "");
if(txt && global)
{
window.clipboardData.setData("Text", txt);
global.focus();
document.execCommand('Paste');
global=null;
}
window.clipboardData.setData("Text", old);
}
</SCRIPT>
按斷行符號提交
<script language="javascript">
<!--
document.onkeydown=function()
{
loginform.submit()
}
//-->
</script>
改變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>點擊以前</div></td></tr></table>
<input type=button value="更改" >
<input type=button value="儲存" >
判斷複選框是否選中
<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('選中的選項按鈕ID:'+o1[i].id);
j++;
}
}
if(j==0) alert('沒選擇選項按鈕!');
for(i=0;i<o2.length;i++)
{
if(o2[i].checked)
{
alert('選中的多選按鈕ID:'+o2[i].id);
p++;
}
}
if(p==0) alert('沒選擇多選按鈕!');
}
//-->
</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>
取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:'+o[i].value);
if(o[i].type == 'checkbox' && o[i].checked) alert('選中複選框的value:'+o[i].value);
}
}
//-->
</script>
全選
<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 >
如何獲得Td的行數
<table border=1>
<tr>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
</tr>
<tr>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
</tr>
<tr>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
<td onclick='alert("行數:"+(this.parentElement.rowIndex+1)+"\n列數:"+(this.cellIndex+1))'>點我</td>
</tr>
</table>
---------------------------------------
根據td,可以如下:
oTd.parentNode.parentNode.parentNode.rows.length 行數
oTd.parentNode.cells.length 列數
------------------------------------------
oTd.parentNode.parentNode.rows.length 行數
如何進行全文替換
<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>
時間格式
<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>
中文標點的判斷
<input type=text name=a>
<input type=button value="提交" >
<script language="JavaScript">
function chk()
{
b=/^\s*[A-Za-z0-9\u4e00-\u9fa5,。;:“”‘’!?、]*\s*$/;
c=document.all.a.value;
if (c.replace(b,"")!="")
{
alert("您輸入了非法字元");
}
}
</script>