写js时遇到的一些小问题


1、在写<asp:Button ……>的OnClientClick事件时,需要加上return;否则会出现OnClientClick返回false,但仍然执行OnClick事件的情况。
例如:
复制代码 代码如下:

<asp:Button ID="btnSearch" runat="server" CssClass="button" Text="查找...."   OnClientClick="return CheckBox();" OnClick="btnSearch_Click"></asp:Button>

2、window.showModalDialog()方式打开新窗口中,如果其中包含js编辑器或者其他第三方编辑器时,会出现不能编辑的情况,连选中都不行;
至今我还没找到比较好的解决办法,还是换成window.open()方式试试。
3、checkbox全选问题:
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基础知识
复制代码 代码如下:


//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中的定时循环调用函数 setInterval (无限弹)和对应的停止函数
var timer //首先要声明一个变量用来存储setInterval返回

的值.
timer=setInterval("alert('123')",500); //用声明好的变量存储setInterval返回的值.
clearInterval(timer); //清除setInterval函数
如果确实要在setInterval方法中调用参数,并且该函数需要传递参数,那么可以通过通过如下方式:把要传的值声明为全局变量,然

后在方法中进行调用.例如:
<script type="text/javaScript">
var n=1,obj ;
function f1()
{ obj =setInterval(f2(),1000) }
function f2()
{ alert(n+=n;)}
</script>



//js获得本页网址
var Url = window.location.href;


//js控制保存本页面
document.execCommand("SaveAs","","C:\\index.htm"); //1.要进行的操作名,2.默认保存到的地址和文件名和文件类型
function save(dizhi){
document.execCommand("SaveAs","",dizhi);
}


//打开,另存为,属性,打印"等14个JS代码
■打开■
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,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(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>
■页面设置■
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置><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()">
■导入收藏■
<input type="button" name="Button" value="导入收藏夹" onClick=window.external.ImportExportFavorites(true,);>
■导出收藏■
<input type="button" name="Button3" value="导出收藏夹" 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=查看源文件>
■语言设置■
<INPUT name=Button onclick="window.external.ShowBrowserUI(LanguageDialog, null)" type=button value=语言设置>
■前进■
<INPUT name=Submit onclick=history.go(1) type=submit value=前进>
■后退■
<INPUT name=Submit2 onclick=history.go(-1) type=submit value=后退>


//改变CSS样式
一、局部改变样式(区分大小写)
改变className(不用加上style)
document.getElementById('t2').className=”…”
改变classText(必须加上style)
document.getElementById('t2').style.cssText=”…”
直接改变控件的CSS的样式
document.getElementById('t2').style.backgroundColor=”#003366″
二、全局改变样式(换肤)
给现在使用的CSS样式加一个id,换肤的时候,更改引用的CSS样式的路径,例如:
<link rel = "stylesheet" type="text/css" id="css" href="firefox.css" />
<span on click="javascript:document.getElementById('css').href = 'ie.css'">点我改变样式</span>


//生成随机数,参数为(最小值,最大值)
function GetRnd(min,max){
return parseInt(Math.random()*(max-min+1));
}


//js中把字符串转化为数字
parseInt("123"); //123

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3