JavaScript常用代码

eval()函数,可将字符串转换为变量对象
var the_image = prompt("change parrot or cheese","");
var the_image_name = "window.document." + the_image;
var the_image_object = eval(the_image_name);
the_image_object.src = "ant.gif"; 

<form action="" method="post" name="myform" onsubmit="return CheckPost();">

myform.user.focus();    (form.conrol.focus()控件获得焦点)

myform.user.value

myform.title.value.length
htmlspecialchars(所要格式化对象,定义单双引号,编码)    //格式化html


1. Jquery文本框限制输入
$("#ctl00_ContentPlaceHolder1_txtZipCodeTwo").keypress(function(e)
{}

数字验证
function Validate_Digit(key)
return (key >= 48 && key <= 57) || key == 0 || key == 8 || key == 13}

2. 验证空
function Validate_Null(msg,srcID)
{
var str = "#" + srcID;
if ($(str)[0].value == "")
{
alert(msg + "不能为空");
var ctrl = document.getElementById(srcID);
ctrl.focus();
return false;
}
return true;
}

4. 限制输入数字
onKeyUp="value = value.replace(/[^0-9]/g,'')"


5. JS在两个窗体间传值 //Hidden1为前一个窗体的隐藏控件
<head>
<title></title>
<script type="text/javascript">
function print()
{
var parent = window.parent.opener;
window.document.body.innerHTML = parent.document.getElementById('Hidden1').value;
}
</script>
</head>
<body onload="print()">

6. JS中的数值转换
parseInt("42.33") //42
parseFloat("42.33") //42.33

7. 父窗体通过document.write在子窗体中生成html代码
<head>
<title></title>
<script type="text/javascript">
var newWindow;
function openNewWind()
{
newWindow = window.open("", "", "status,height=200,width=200");
}

function subWrite()
{
if (newWindow.closed)
openNewWind();
newWindow.focus();

var newContent = "<html><head><title>hello</title></head><body bgcolor='red'><h1>你好吗</h1></body></html>";
newWindow.document.write(newContent);
newWindow.document.close();
}
</script>
</head>
<body onload="openNewWind()">
<input id="Button1" type="button" value="button" onclick="subWrite()" />
</body>

5. 窗体全屏
function fullScreen()
{
window.moveTo(0, 0);
window.resizeTo(screen.availWidth, screen.availHeight);
}

6. 锚的使用
<a href="#C4">See also Chapter 4. </a>   //跳转
<a name="C4"> </a> //被跳转的地方

7. 跳出框架

if (window.top!=window.self) 
    {
    window.top.location="tryjs_breakout.htm"
    }
}
原文地址:https://www.cnblogs.com/gossip/p/2259511.html