---
title: "js使用小技巧"
author: "bobshute"
date: "2009-04-07T21:25:25Z"
tags: ["CSS", "Excel", "Microsoft", "fckeditor", "prototype"]
slug: "js"
draft: false
lastmod: "2009-04-07T21:25:25Z"
id: 363028
url: "https://bobshute.iteye.com/blog/363028"
---

1.收藏时用图标显示  
&lt;link REL="SHORTCUT ICON" href="http://www.asp888.net/site.ico"&gt;  
2.TD内容实现不换行  
只要使用noWrap属性即可  
3.获得客户端文件大小  
&lt;img src="http://www.webucn.com/web/images/banner.gif" onmouseover="this.alt='文件大小为：'+this.fileSize+'字节'"&gt;  
4.动态删除元素  
img1.removeNode(true)  
5.模拟按键：  
var WshShell = new ActiveXObject("WScript.Shell");  
WshShell.sendKeys("{F11}");  
6.以下语句不需要纠错判断  
for(i=2;i&lt;1;i++){}  
7.把js文件群打包掉用  
&lt;script archive=utils.jar src=animation.js&gt;&lt;/script&gt;  
8.获得当前选中文本  
document.getSelection()  
9.获得对象在document.all\[]中元素的下标  
txt1.sourceIndex  
10.滚动文档到窗口顶部或底部  
txt1.scrollIntoView(true)  
11.段落自动缩进  
&lt;p style=text-indent:26px&gt;内容&lt;/p&gt;  
12.客户端IP地址  
Request.ServerVariables("REMOTE\_HOST")  
13.元素投影(可彻底解决网页中投影问题)  
filter:progid:DXImageTransform.Microsoft.Shadow(color='gray',Direction=135, Strength=3)  
14.图片热区焦点隐藏  
&lt;img hideFocus&gt;  
15.不能在onload事件中open窗口，否则会被拦截  
16.选择本地文件时改变图片源  
&lt;input type="file" onpropertychange="img.src=value"&gt;  
17.获取文件路径  
var str = "http://localhost/Home/manage/FCKeditor/dialog/fck\_image.html"; str = str.replace(/\[^\\/]+$/,"");  
18.js中的trim  
String.prototype.trim = function()  
{  
  return this.replace(/(^\[\\s]\*)|(\[\\s]\*$)/g, "");  
}  
19.定义样式表的子元素样式  
.cls button{color:red}  
21.文本框tab  
&lt;textarea onkeydown="if(event.keyCode==9){clipboardData.setData('Text', '    ');execCommand('paste');return false}" rows=8 cols=28&gt;  
&lt;/textarea&gt;  
22.最大化窗口  
&lt;OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT&gt;&lt;PARAM name="Command" value="Maximize"&gt;&lt;/OBJECT&gt;  
23.图片title换行符  
&#13;  
24.对body的click事件重定义  
document.body.onclick=function(){ alert() }  
25.10进制转换为16进制  
alert((255).toString(16))  
26.隐藏链接提示  
&lt;style&gt;    
a  {evt:expression(window.status='')}    
&lt;/style&gt;    
&lt;a  href=http://ywicc.com&gt;no  status  information&lt;/a&gt;  
注意：expression为获焦或失焦时执行，可为寄主创建一参数，使其只执行一次，用于表格控制  
27.同时执行两事件  
function document.onclick,document.onkeydown(){  
   window.alert('Hello World!!!');  
}  
28.不显示图片工具栏  
&lt;meta http-equiv="imagetoolbar" content="no"&gt;  
29.页面最大化  
&lt;OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT&gt;&lt;PARAM name="Command" value="Maximize"&gt;&lt;/OBJECT&gt;  
30.提交到新的可自义窗口  
&lt;form method="post" action="1.htm" target="test" onSubmit="window.open('','test','width=100,height=100')"&gt;  
&lt;input type="submit"&gt;  
&lt;/form&gt;  
31.用css控制表格间隔色  
tr{background:expression(this.rowIndex%2==0 ? "red":"")}  
32.取得字符的asc码  
"s".charCodeAt(0)  
33.获得行对象所在行数  
o.rowIndex  
34.去掉最后一个字符  
s.slice(0,-1)  
35.获得系统背景色  
style=background:buttonface  
36.表格导出为excel文件  
客户端提供链接，在服务器端输出xls文件，由客户保存到客户端即可  
服务器端改变文件名的方法是  
Response.ContentType = "application/vnd.ms-excel"  
Response.AddHeader("Content-Disposition", "attachment;filename=demo.exe")  
37.在模式窗口中链接  
&lt;base target="\_self"&gt;  
38.求两点夹角  
Math.atan2(y1-y0, x1-x0)\*180/Math.PI