Js编程小技巧(不断积累中)

来源:互联网 发布:北大青鸟软件培训课程 编辑:程序博客网 时间:2024/06/14 13:44

 在项目开发的时候,经常会要求实现一些js方面的小功能。因此记录在这里供以后参考:

1.javascript 打开CHM文件

     window.showHelp("../jscript56.chm");

2.一个免费的天气预报插件

将以下代码拷入页面就可以直接显示

<iframe src="http://m.weather.com.cn/m/pn12/weather.htm " width="245" height="110" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no"></iframe>

3.日历控件:http://www.goocnjp.com/data/tools/calendar.html

4.创建表头固定,表体可滚动的GridView

<script type="text/javascript">
function s()
{
 var t = document.getElementById("<%=GridView1.ClientID%>");
 var t2 = t.cloneNode(true)
 for(i = t2.rows.length -1;i > 0;i--)
 t2.deleteRow(i) 
 t.deleteRow(0) 
 a.appendChild(t2)
}
window.onload = s
</script>

<form id="Form1" runat="server">
    <table>
      <tr>
        <td>
          <div id="a">
          </div>
          <div style="overflow-y: scroll; height: 200px;table-layout:fixed">
            <asp:GridView ID="GridView1" runat="server" Font-Size="12px" BackColor="#FFFFFF"
              GridLines="Both" CellPadding="4" Width="560">
              <HeaderStyle BackColor="#EDEDED" Height="26px" />
            </asp:GridView>
          </div>
        </td>
      </tr>
    </table>
  </form>

 5.防止页面被拷贝的方法

在body中加入下面代码,再加上去掉工具栏,菜单,别人就很难拷贝你的代码

<Body onselectstart="return false" onmousemove = "document.selection.empty();" onmouseup="document.selection.empty();" oncopy ="document.selection.empty();" onselect = "document.selection.empty();"  oncontextmenu="return false">

 6.IE6 情况下让PNG图片透明的方法

<script language="javascript">// 修复 IE 下 PNG 图片不能透明显示的问题function fixPNG(myImage) {var arVersion = navigator.appVersion.split("MSIE");var version = parseFloat(arVersion[1]);if ((version >= 5.5) && (version < 7) && (document.body.filters)){     var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";     var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";     var imgTitle = (myImage.title) ? "title='" + myImage.title   + "' " : "title='" + myImage.alt + "' ";     var imgStyle = "display:inline-block;" + myImage.style.cssText;     var strNewHTML = "<span " + imgID + imgClass + imgTitle   + " style=\"" + "width:" + myImage.width   + "px; height:" + myImage.height   + "px;" + imgStyle + ";"   + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"   + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>";     myImage.outerHTML = strNewHTML;} } window.onload=function(){         document.getElementById("top").style.height=screen.height/5+"px";        }//</script>用法如下:<img src="logo.png" width="328" height="325" border="0" onload="fixPNG(this)" /> 

 

7.多为数四舍五入的通用方法:

//num表示要的数,v表示要保留的小数位数。
function decimal(num,v)
{
    var vv = Math.pow(10,v);
    return Math.round(num*vv)/vv;
}

 

原创粉丝点击