您的位置:首页 > Web前端 > JavaScript

Javascript部分常用技巧

2012-12-12 17:32 323 查看
本文转载自谈笑涧《Javascript部分常用技巧》

定时自动转页
<script>setTimeout('document.location.href="[url]"',5000)</script>

返回前一页
<a href="javascript:history.back()">返 回</a>

关闭窗口
<script>javascript:window.close()</script>

弹出消息窗口
<script>window.alert("hi!");</script>")

定焦点到文本框的末尾
function focusEnd(){
if(field.createTextRange){
var r = field.reateTextRange();
r.moveStart('character',field.value.length);
r.collapse();
r.select();
}
}

规范帖子中的图片
<img onload='javascript:if(this.width>790)this.style.width=790;'
onclick='javascript:window.open(this.src);'
style='CURSOR:hand'
onmousewheel='return bbimg(this)'>
function bbimg(o){
var zoom=parseInt(o.style.zoom, 10)||100;zoom+=event.wheelDelta/12;if (zoom>0) o.style.zoom=zoom+'%';
return false;
}

js文件中的中文问题
web application的编码规则是utf-8,那么js文件中如果有中文就会出现乱码(有可能是其它的页面不正常的情况)
解决这个问题请在引用javascript的地方加上charset="gb2312"或者charset="big5"
例如:<script type="text/javascript" language="javascript" src="scripts/function.js" charset="gb2312"></script>

窗口无边框
<a href="#" onclick="window.open('test.htm','','width=300,height=450,toolbar=0,menubar=0,location=0')">打开</a>

输入框长度随输入内容的长度而改变
<input type="text" style="overflow-x:visible;width:60;">

点击单选框就直接激活链接
第一步:把如下代码加入到<head>区域中
<SCRIPT>
function go(loc) {
window.location.href = loc;
}
</script>
第二步:把如下代码加入到<body>区域中
<form name="form">
<input type="radio" name="loc" onClick="go('#');">
选中即激发了超链接<br>
<input type="radio" name="loc" onClick="go('#l');">
选中即激发了超链接<br>
<input type="radio" name="loc" onClick="go('#');">
选中即激发了超链接<br>
</form>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: