寫js時(shí)遇到的一些小問(wèn)題
更新時(shí)間:2010年12月06日 22:45:03 作者:
寫js時(shí)遇到的一些小問(wèn)題,需要的朋友可以參考下。
1、在寫<asp:Button ……>的OnClientClick事件時(shí),需要加上return;否則會(huì)出現(xiàn)OnClientClick返回false,但仍然執(zhí)行OnClick事件的情況。
例如:
<asp:Button ID="btnSearch" runat="server" CssClass="button" Text="查找...." OnClientClick="return CheckBox();" OnClick="btnSearch_Click"></asp:Button>
2、window.showModalDialog()方式打開(kāi)新窗口中,如果其中包含js編輯器或者其他第三方編輯器時(shí),會(huì)出現(xiàn)不能編輯的情況,連選中都不行;
至今我還沒(méi)找到比較好的解決辦法,還是換成window.open()方式試試。
3、checkbox全選問(wèn)題:
html:
<input type="checkbox" id="chkAll" onclick="selectAll()" />全選
<asp:CheckBoxList ID="ckblist" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal"></asp:CheckBoxList>
js:
function selectAll()
{
var obj = document.getElementById("chkAll");
var ckblist = document.getElementById("ckblist");
var chk = ckblist.getElementsByTagName("input");
if(obj.checked)
{
for(var i = 0; i < chk.length; i++)
{
chk[i].checked = true;
}
}
else
{
for(var i = 0; i < chk.length; i++)
{
chk[i].checked = false;
}
}
}
下面是一些js基礎(chǔ)知識(shí)
①
//js獲取Table并改變它的樣式
<script type="text/javascript">
function setColSpan()
{
var x=document.getElementById('myTable').rows[0].cells //獲取table的第一行
x[0].colSpan="1" //改變table的樣式
x[1].colSpan="3" //同上
}
</script>
<table id="myTable" border="1">
<tr>
<td colspan="2">單元格1</td>
<td colspan="2">單元格2</td>
</tr>
<tr>
<td>單元格3</td>
<td>單元格4</td>
<td>單元格5</td>
<td>單元格6</td>
</tr>
</table>
<input type="button" onclick="setColSpan()" value="改變colspan值">
②
//js中的定時(shí)循環(huán)調(diào)用函數(shù) setInterval (無(wú)限彈)和對(duì)應(yīng)的停止函數(shù)
var timer //首先要聲明一個(gè)變量用來(lái)存儲(chǔ)setInterval返回
的值.
timer=setInterval("alert('123')",500); //用聲明好的變量存儲(chǔ)setInterval返回的值.
clearInterval(timer); //清除setInterval函數(shù)
如果確實(shí)要在setInterval方法中調(diào)用參數(shù),并且該函數(shù)需要傳遞參數(shù),那么可以通過(guò)通過(guò)如下方式:把要傳的值聲明為全局變量,然
后在方法中進(jìn)行調(diào)用.例如:
<script type="text/javaScript">
var n=1,obj ;
function f1()
{ obj =setInterval(f2(),1000) }
function f2()
{ alert(n+=n;)}
</script>
③
//js獲得本頁(yè)網(wǎng)址
var Url = window.location.href;
④
//js控制保存本頁(yè)面
document.execCommand("SaveAs","","C:\\index.htm"); //1.要進(jìn)行的操作名,2.默認(rèn)保存到的地址和文件名和文件類型
function save(dizhi){
document.execCommand("SaveAs","",dizhi);
}
⑤
//打開(kāi),另存為,屬性,打印"等14個(gè)JS代碼
■打開(kāi)■
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打開(kāi)>
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■另存為■
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存為><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■屬性■
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=屬性><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■打印■
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■頁(yè)面設(shè)置■
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=頁(yè)面設(shè)置><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■刷新■
<input type=button value=刷新 name=refresh onclick="window.location.reload()">
■導(dǎo)入收藏■
<input type="button" name="Button" value="導(dǎo)入收藏夾" onClick=window.external.ImportExportFavorites(true,);>
■導(dǎo)出收藏■
<input type="button" name="Button3" value="導(dǎo)出收藏夾" onClick=window.external.ImportExportFavorites(false,);>
■加入收藏■
<INPUT name=Button2 onclick="window.external.AddFavorite(location.href, document.title)" type=button value=加入收藏
夾>
■整理收藏夾■
<INPUT name=Submit2 onclick="window.external.ShowBrowserUI(OrganizeFavorites, null)" type=button value=整理收藏夾>
■查看原文件■
<INPUT name=Button onclick=window.location = "view-source:" + window.location.href type=button value=查看源文件>
■語(yǔ)言設(shè)置■
<INPUT name=Button onclick="window.external.ShowBrowserUI(LanguageDialog, null)" type=button value=語(yǔ)言設(shè)置>
■前進(jìn)■
<INPUT name=Submit onclick=history.go(1) type=submit value=前進(jìn)>
■后退■
<INPUT name=Submit2 onclick=history.go(-1) type=submit value=后退>
⑥
//改變CSS樣式
一、局部改變樣式(區(qū)分大小寫)
改變className(不用加上style)
document.getElementById('t2').className=”…”
改變classText(必須加上style)
document.getElementById('t2').style.cssText=”…”
直接改變控件的CSS的樣式
document.getElementById('t2').style.backgroundColor=”#003366″
二、全局改變樣式(換膚)
給現(xiàn)在使用的CSS樣式加一個(gè)id,換膚的時(shí)候,更改引用的CSS樣式的路徑,例如:
<link rel = "stylesheet" type="text/css" id="css" href="firefox.css" />
<span on click="javascript:document.getElementById('css').href = 'ie.css'">點(diǎn)我改變樣式</span>
⑦
//生成隨機(jī)數(shù),參數(shù)為(最小值,最大值)
function GetRnd(min,max){
return parseInt(Math.random()*(max-min+1));
}
⑧
//js中把字符串轉(zhuǎn)化為數(shù)字
parseInt("123"); //123
例如:
復(fù)制代碼 代碼如下:
<asp:Button ID="btnSearch" runat="server" CssClass="button" Text="查找...." OnClientClick="return CheckBox();" OnClick="btnSearch_Click"></asp:Button>
2、window.showModalDialog()方式打開(kāi)新窗口中,如果其中包含js編輯器或者其他第三方編輯器時(shí),會(huì)出現(xiàn)不能編輯的情況,連選中都不行;
至今我還沒(méi)找到比較好的解決辦法,還是換成window.open()方式試試。
3、checkbox全選問(wèn)題:
html:
復(fù)制代碼 代碼如下:
<input type="checkbox" id="chkAll" onclick="selectAll()" />全選
<asp:CheckBoxList ID="ckblist" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal"></asp:CheckBoxList>
js:
復(fù)制代碼 代碼如下:
function selectAll()
{
var obj = document.getElementById("chkAll");
var ckblist = document.getElementById("ckblist");
var chk = ckblist.getElementsByTagName("input");
if(obj.checked)
{
for(var i = 0; i < chk.length; i++)
{
chk[i].checked = true;
}
}
else
{
for(var i = 0; i < chk.length; i++)
{
chk[i].checked = false;
}
}
}
下面是一些js基礎(chǔ)知識(shí)
復(fù)制代碼 代碼如下:
①
//js獲取Table并改變它的樣式
<script type="text/javascript">
function setColSpan()
{
var x=document.getElementById('myTable').rows[0].cells //獲取table的第一行
x[0].colSpan="1" //改變table的樣式
x[1].colSpan="3" //同上
}
</script>
<table id="myTable" border="1">
<tr>
<td colspan="2">單元格1</td>
<td colspan="2">單元格2</td>
</tr>
<tr>
<td>單元格3</td>
<td>單元格4</td>
<td>單元格5</td>
<td>單元格6</td>
</tr>
</table>
<input type="button" onclick="setColSpan()" value="改變colspan值">
②
//js中的定時(shí)循環(huán)調(diào)用函數(shù) setInterval (無(wú)限彈)和對(duì)應(yīng)的停止函數(shù)
var timer //首先要聲明一個(gè)變量用來(lái)存儲(chǔ)setInterval返回
的值.
timer=setInterval("alert('123')",500); //用聲明好的變量存儲(chǔ)setInterval返回的值.
clearInterval(timer); //清除setInterval函數(shù)
如果確實(shí)要在setInterval方法中調(diào)用參數(shù),并且該函數(shù)需要傳遞參數(shù),那么可以通過(guò)通過(guò)如下方式:把要傳的值聲明為全局變量,然
后在方法中進(jìn)行調(diào)用.例如:
<script type="text/javaScript">
var n=1,obj ;
function f1()
{ obj =setInterval(f2(),1000) }
function f2()
{ alert(n+=n;)}
</script>
③
//js獲得本頁(yè)網(wǎng)址
var Url = window.location.href;
④
//js控制保存本頁(yè)面
document.execCommand("SaveAs","","C:\\index.htm"); //1.要進(jìn)行的操作名,2.默認(rèn)保存到的地址和文件名和文件類型
function save(dizhi){
document.execCommand("SaveAs","",dizhi);
}
⑤
//打開(kāi),另存為,屬性,打印"等14個(gè)JS代碼
■打開(kāi)■
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打開(kāi)>
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■另存為■
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存為><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■屬性■
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=屬性><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■打印■
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■頁(yè)面設(shè)置■
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=頁(yè)面設(shè)置><OBJECT
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
■刷新■
<input type=button value=刷新 name=refresh onclick="window.location.reload()">
■導(dǎo)入收藏■
<input type="button" name="Button" value="導(dǎo)入收藏夾" onClick=window.external.ImportExportFavorites(true,);>
■導(dǎo)出收藏■
<input type="button" name="Button3" value="導(dǎo)出收藏夾" onClick=window.external.ImportExportFavorites(false,);>
■加入收藏■
<INPUT name=Button2 onclick="window.external.AddFavorite(location.href, document.title)" type=button value=加入收藏
夾>
■整理收藏夾■
<INPUT name=Submit2 onclick="window.external.ShowBrowserUI(OrganizeFavorites, null)" type=button value=整理收藏夾>
■查看原文件■
<INPUT name=Button onclick=window.location = "view-source:" + window.location.href type=button value=查看源文件>
■語(yǔ)言設(shè)置■
<INPUT name=Button onclick="window.external.ShowBrowserUI(LanguageDialog, null)" type=button value=語(yǔ)言設(shè)置>
■前進(jìn)■
<INPUT name=Submit onclick=history.go(1) type=submit value=前進(jìn)>
■后退■
<INPUT name=Submit2 onclick=history.go(-1) type=submit value=后退>
⑥
//改變CSS樣式
一、局部改變樣式(區(qū)分大小寫)
改變className(不用加上style)
document.getElementById('t2').className=”…”
改變classText(必須加上style)
document.getElementById('t2').style.cssText=”…”
直接改變控件的CSS的樣式
document.getElementById('t2').style.backgroundColor=”#003366″
二、全局改變樣式(換膚)
給現(xiàn)在使用的CSS樣式加一個(gè)id,換膚的時(shí)候,更改引用的CSS樣式的路徑,例如:
<link rel = "stylesheet" type="text/css" id="css" href="firefox.css" />
<span on click="javascript:document.getElementById('css').href = 'ie.css'">點(diǎn)我改變樣式</span>
⑦
//生成隨機(jī)數(shù),參數(shù)為(最小值,最大值)
function GetRnd(min,max){
return parseInt(Math.random()*(max-min+1));
}
⑧
//js中把字符串轉(zhuǎn)化為數(shù)字
parseInt("123"); //123
相關(guān)文章
微信小程序購(gòu)物車、父子組件傳值及calc的注意事項(xiàng)總結(jié)
這篇文章主要給大家介紹了關(guān)于微信小程序購(gòu)物車、父子組件傳值及calc的注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11JS實(shí)現(xiàn)的簡(jiǎn)單折疊展開(kāi)動(dòng)畫(huà)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)單折疊展開(kāi)動(dòng)畫(huà)效果,可實(shí)現(xiàn)類似百度頁(yè)面分享按鈕一樣的折疊展開(kāi)動(dòng)畫(huà)效果,涉及javascript頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04javascript阻止scroll事件多次執(zhí)行的思路及實(shí)現(xiàn)
阻止scroll事件多次執(zhí)行主要是為了解決一些常見(jiàn)網(wǎng)頁(yè)特效在js解析時(shí)預(yù)期與效果不同,感興趣的朋友可以了解下2013-11-11用javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了用javascript實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08javascript中的相等操作符(==與===區(qū)別)
這篇文章主要介紹了javascript中的相等操作符(==與===區(qū)別),需要的朋友可以參考下2019-12-12