`

JavaScript使用小技巧

阅读更多
    http://xuebinxu.blog.51cto.com/97412/23680
1.收藏时用图标显示
<link REL="SHORTCUT ICON" href="http://www.asp888.net/site.ico">
2.TD内容实现不换行
只要使用noWrap属性即可
3.获得客户端文件大小
<img src="http://www.webucn.com/web/images/banner.gif" onmouseover="this.alt='文件大小为:'+this.fileSize+'字节'">
4.动态删除元素
img1.removeNode(true)

5.模拟按键:

var WshShell = new ActiveXObject("WScript.Shell");
WshShell.sendKeys("{F11}");

6.以下语句不需要纠错判断
for(i=2;i<1;i++){}
7.把js文件群打包掉用
<script archive=utils.jar src=\'#\'" /script>
8.获得当前选中文本
document.getSelection()
9.获得对象在document.all[]中元素的下标
txt1.sourceIndex
10.滚动文档到窗口顶部或底部
txt1.scrollIntoView(true)
11.段落自动缩进
<p style=text-indent:26px>内容</p>
12.客户端IP地址
Request.ServerVariables("REMOTE_HOST")
13.元素投影(可彻底解决网页中投影问题)
filter:progid:DXImageTransform.Microsoft.Shadow(color='gray',Direction=135, Strength=3)
14.图片热区焦点隐藏
<img hideFocus>
15.不能在onload事件中open窗口,否则会被拦截
16.选择本地文件时改变图片源
<input type="file" onpropertychange="img.src=\'#\'" /font>
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
<textarea onkeydown="if(event.keyCode==9){clipboardData.setData('Text', '    ');execCommand('paste');return false}" rows=8 cols=28>
</textarea>
22.最大化窗口
<OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT><PARAM name="Command" value="Maximize"></OBJECT>
23.图片title换行符
&#13;
24.对body的click事件重定义
document.body. alert() }
25.10进制转换为16进制
alert((255).toString(16))
26.隐藏链接提示
<style> 
a  {evt:expression(window.status='')} 
</style> 
<a  href=http://ywicc.com>no  status  information</a>
注意:expression为获焦或失焦时执行,可为寄主创建一参数,使其只执行一次,用于表格控制
27.同时执行两事件
function document.onclick,document.onkeydown(){
   window.alert('Hello World!!!');
}
28.不显示图片工具栏
<meta http-equiv="imagetoolbar" content="no">
29.页面最大化
<OBJECT classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" onreadystatechange="if (this.readyState==4) this.Click();" VIEWASTEXT><PARAM name="Command" value="Maximize"></OBJECT>
30.提交到新的可自义窗口
<form method="post" action="1.htm" target="test" onSubmit="window.open('','test','width=100,height=100')">
<input type="submit">
</form>
31.用css控制表格间隔色
tr{background:expression=\'#\'" ? "red":"")}
32.取得字符的asc码
"s".charCodeAt(0)
33.获得行对象所在行数
o.rowIndex
34.去掉最后一个字符
s.slice(0,-1)
35.获得系统背景色
style=background=\'#\'" /font>
36.表格导出为excel文件
客户端提供链接,在服务器端输出xls文件,由客户保存到客户端即可
服务器端改变文件名的方法是
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=demo.exe")
37.在模式窗口中链接
<base target="_self">
38.求两点夹角
Math.atan2(y1-y0, x1-x0)*180/Math.PI
39.浏览器不支持的时候显示  <noscript> Hello to the non-JavaScript browser.</noscript>
40.输出到浏览器  document.write(“<strong>Hello</strong>”);
41.字符串搜索  <script language=”JavaScript”><!-- var myVariable = “Hello there”;
var therePlace = myVariable.search(“there”); document.write(therePlace);// --> </script>
42.格式化字串 <script language=”JavaScript”><!—
var myVariable = “Hello there”; document.write(myVariable.big() + “<br>”);
document.write(myVariable.blink() + “<br>”); document.write(myVariable.bold() + “<br>”);
document.write(myVariable.fixed() + “<br>”);document.write(myVariable.fontcolor(“red”) + “<br>”);
document.write(myVariable.fontsize(“18pt”) + “<br>”);document.write(myVariable.italics() + “<br>”);
document.write(myVariable.small() + “<br>”);document.write(myVariable.strike() + “<br>”);
document.write(myVariable.sub() + “<br>”);document.write(myVariable.sup() + “<br>”);
document.write(myVariable.toLowerCase() + “<br>”); document.write(myVariable.toUpperCase() + “<br>”);
var firstString = “My String”; var finalString = firstString.bold().toLowerCase().fontcolor(“red”);
// --></script>
43.取消定时执行 <script> <!-- function hello() { window.alert(“Hello”); }
var myTimeout = window.setTimeout(“hello()”,5000); window.clearTimeout(myTimeout);// --> </script>
44.读取URL参数 <script language=”JavaScript”> var urlParts = document.URL.split(“?”);
var parameterParts = urlParts[1].split(“&”);for (i = 0; i < parameterParts.length; i++) {
var pairParts = parameterParts[i].split(“=”);var pairName = pairParts[0]; var pairValue = pairParts[1];
document.write(pairName + “ :“ +pairValue ); }</script>
你还以为HTML是无状态的么?
45.打开一个新的document对象 <script language=”JavaScript”> function newDocument() {document.open();
document.write(“<p>This is a New Document.</p>”);document.close(); }</script>
46.简单的图像替换 <script language=”JavaScript”>rollImage = new Image; rollImage.src = “rollImage1.jpg”;
defaultImage = new Image;defaultImage.src = “image1.jpg”; </script>
<a href="/”myUrl"” onMouseOver=”document.myImage.src = rollImage.src;”
onMouseOut=”document.myImage.src = defaultImage.src;”>
<img src="/”image1.jpg"” name=”myImage” width=100 height=100 border=0>

47.动态增加Select项 <form name=”myForm”><select name=”mySelect”><option value=”First Choice”>1</option>
<option value=”Second Choice”>2</option></select></form><script language=”JavaScript”>
document.myForm.mySelect.length++;document.myForm.mySelect.options[document.myForm.mySelect.length - 1].text = “3”;
document.myForm.mySelect.options[document.myForm.mySelect.length - 1].value = “Third Choice”;</script>

48.改变浏览器状态栏文字提示<script language=”JavaScript”> window.status = “A new status message”;</script>
49.往新窗口中写内容 <script language=”JavaScript”> var newWindow = window.open(“”,”newWindow”);
newWindow.document.open(); newWindow.document.write(“This is a new window”); newWIndow.document.close();</script>

50. 加载页面到框架页面 <frameset cols=”50%,*”> <frame name=”frame1” src="/”135a.html"”>
<frame name=”frame2” src="/”about:blank"”></frameset>
在frame1中加载frame2中的页面 parent.frame2.document.location = “135b.html”;
51. 在框架页面之间共享脚本   如果在frame1中html文件中有个脚本 function doAlert() { window.alert(“Frame 1 is loaded”);}
那么在frame2中可以如此调用该方法<body onLoad=”parent.frame1.doAlert();”>This is frame 2.</body>
52. 数据公用   可以在框架页面定义数据项,使得该数据可以被多个框架中的页面公用
<script language=”JavaScript”>var persistentVariable = “This is a persistent value”;</script>
<frameset cols=”50%,*”><frame name=”frame1” src="/”138a.html"”><frame name=”frame2” src="/”138b.html"”> </frameset>这样在frame1和frame2中都可以使用变量persistentVariable
53. 框架代码库   根据以上的一些思路,我们可以使用一个隐藏的框架页面来作为整个框架集的代码库
<frameset cols=”0,50%,*”> <frame name=”codeFrame” src="/”140code.html"”> <frame name=”frame1” src="/”140a.html"”>
<frame name=”frame2” src="/”140b.html"”></frameset>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics