日期:2014-05-16  浏览次数:20285 次

js实用的小小小技巧
一:快速取得当前form里的input值
1)document.getElementById('u_name');
2) document.forms[0].u_name;
3) document.form(0).u_name

二:整了半天才知道原来在struts1.2.9中对checkbox和radio等的id设置是用styleId来定义。郁闷,浪费时间啊……

三:在一个form表单中,submit后希望返回值在一个新的窗口上,则可以动态地对该form的属性target设置成'_blank'即:
document.forms[0].target='_blank';//forms[0]是第一个form


四:IE中事件的起泡
今天看到这么一则文章,然后就调试了一下http://x8.maicoo.com/tech/javascript/17262.html
在html里这么写:
<div onclick='go("http://www.google.com.cn");'>
	<a href="#" onclick='go("http://www.baidu.com");'>Go ===></a>
</div>

js这么写:
function go(url){
		alert(url);
		window.location.href = url;
	}

用Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4调试更直接

五:对新窗口的控制
有时候希望显示的内容在新的窗口上,可以这样控制。类似一般js站点上点击:"运行代码"按钮的功能
function wopen(){
 var text = getElementById('infomation').innerHtml;
 var win = window.open('','_blank','');
 win.document.open('text/html','hcq');
 win.document.writeln(text);
 win.document.close();
}


六:改变html文档里的title
最近用QQ邮箱里的聊天功能,发现把网页最小化的时候也,当消息来的时候让title的内容滚动起来就可以达到提示用户的效果。改变title真是简单
document.title='msg';//一句话就OK
当然也可以再写一个让它滚动的函数,这样效果就更好了。

七:给定时器调用传递参数(摘自:征服Ajax——Web 2.0开发技术详解)
<script language="JavaScript" type="text/javascript">
<!--
var userName="jack";
//根据用户名显示欢迎信息
function hello(_name){
       alert("hello,"+_name);
}
//创建一个函数,用于返回一个无参数函数
function _hello(_name){
       return function(){
             hello(_name);
       }
}
window.setTimeout(_hello(userName),3000);
//-->
</script>


八:改变table里的鼠标划过的颜色
<tr onmouseover='this.style.color="#e7fbff"' onmouseout='this.style.color="#FFFFFF"' >
……
</tr>
最好是把和样式设置相关的都放进css里,以后改了方便。