一些常见的问题与解决代码!(精典) 2

屏蔽右键
<a href="http://ywicc.com" onmousedown="if(event.button==2)alert('禁止')">ywicc.com</a>

网页屏保
<script  language="JavaScript">
function  screensave(){
test.value++;
if(test.value==5){
test.style.display='none';
document.all[4].bgColor='black';
}
}
function  screenopen(){
test.value=0;
test.style.display='';
document.all[4].bgColor='';
}
</script>
<body  onkeydown="screenopen()"  onmousemove="screenopen()"  onload="setInterval('screensave()',1000)">
5  秒屏保<input  id="test">


定义打开网页时起始窗口的大小
<script for="window" event="onload">
window.resizeTo(500,300)
</script>

输入字母、数字的正则表达式
<input onkeyup="this.value = this.value.replace(/[^a-zA-Z\d]/g,'')"  onbeforepaste="this.value = this.value.replace(/[^a-zA-Z\d]/g,'')">

不随页面滚动的层
<body onscroll="d1.style.top=document.body.scrollTop+10">
<div id="d1" style="position:absolute; top:10px; 100px; height:100px; background-color:red;"></div>
<img width="1" height="1000">


一个表单里的同一个按钮提交两个页面
<script language="JavaScript">
function testform(){
window.open('','t1');
form1.action='1.htm';
form1.target='t1';
form1.submit();
window.open('','t2');
form1.action='2.htm';
form1.target='t2';
form1.submit();
window.open('','t3');
form1.action='3.htm';
form1.target='t3';
form1.submit();
}
</script>
<form name="form1">
<input name="test">
<input type="button" value="submit" onClick="testform()">
</form>

 判断是不是数字
<INPUT TYPE="text" NAME="num"><INPUT TYPE="button" name="test" onclick="testNum()">
<SCRIPT LANGUAGE="JavaScript">
<!--
function testNum(){
var pattern=/^\d+$/g;
if (!(pattern.test(num.value)))
{alert("please input a number!")}
}
//-->
</SCRIPT>


 alt和title的分行
<a href="#" title="换换换
行行行">test</a>


取消选取、防止复制
<body onselectstart="return false">
<p>111111111111111111111111111111111</p>
</body>


关闭输入法
<input style="ime-mode:disabled">


永远都会带着框架
<script language="JavaScript"><!--
if (window == top)top.location.href = "frames.htm"; //frames.htm为框架网页
// --></script>


防止被人frame
<SCRIPT LANGUAGE=JAVASCRIPT><!--
if (top.location != self.location)top.location=self.location;
// --></SCRIPT>


网页将不能被另存为
<noscript><iframe src=*.html></iframe></noscript>


删除时确认

<a href='javascript:if(confirm("确实要删除吗?"))location="boos.asp?&areyou=删除&page=1"'>删除</a>



取得控件的绝对位置

//Javascript
<script language="Javascript">
function getIE(e){
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
alert("top="+t+"/nleft="+l);
}
</script>

//VBScript
<script language="VBScript"><!--
function getIE()
dim t,l,a,b
set a=document.all.img1
t=document.all.img1.offsetTop
l=document.all.img1.offsetLeft
while a.tagName<>"BODY"
set a = a.offsetParent
t=t+a.offsetTop
l=l+a.offsetLeft
wend
msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"
end function
--></script>



光标是停在文本框文字的最后
<script language="javascript">
function cc()屏蔽功能键Shift,Alt,Ctrl
<script>
function look(){
if(event.shiftKey)
alert("禁止按Shift键!"); //可以换成ALT CTRL
}
document.onkeydown=look;
</script>


网页不会被缓存
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
或者<META HTTP-EQUIV="expires" CONTENT="0">


怎样让表单没有凹凸感
<input type=text style="border:1 solid #000000">

<input type=text style="border-left:none; border-right:none; border-top:none; border-bottom: 1 solid #000000"></textarea>


 <div><span>&<layer>的区别
<div>(division)用来定义大段的页面元素,会产生转行
<span>用来定义同一行内的元素,跟<div>的唯一区别是不产生转行
<layer>是ns的标记,ie不支持,相当于<div>

让弹出窗口总是在最上面
<body onblur="this.focus();">


不要滚动条

让竖条没有:

<body style='overflow:scroll;overflow-y:hidden'>
</body>

让横条没有:

<body style='overflow:scroll;overflow-x:hidden'>
</body>

两个都去掉?更简单了

<body scroll="no">
</body>


怎样去掉图片链接点击后,图片周围的虚线
<a href="#" onFocus="this.blur()"><img src="http://www.51js.com/images/51js/51js.gif" border=0></a>


电子邮件处理提交表单
<form name="form1" method="post" action="mailt****@***.com" enctype="text/plain">
<input type=submit>
</form>


子窗口刷新父窗口
window.opener.location.reload()

在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动
<STYLE>
body
{background-image:url(images/51js/51js.gif); background-repeat:no-repeat; background-position:center;background-attachment: fixed}
</STYLE>

检查一段字符串是否全由数字组成
<script language="Javascript"><!--
function checkNum(str){return str.match(//D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>

 限制下拉选择的宽度
<select name="select" style="300"></select>


打开一个新的窗口,而窗口的宽度和高度则是这个图片的实际的高和宽
<img src="http://www.51js.com/images/51js/51js.gif" onClick="window.open('','','width='+this.width+',height='+this.height)">


弹出的窗口居中

方法一:

<script>
myWin=window.open("about:blank","","width=200,height=160");
myWin.moveTo(screen.width/2-100,screen.height/2-80);
</script>

方法二:

<script>
t = 200;
l = 200;
window.open('about:blank','','width='+l+',height='+t+',top='+(screen.availHeight/2-t/2)+',left='+(screen.availWidth/2-l/2));
</script>


原文地址:https://www.cnblogs.com/swordzj/p/2034799.html