//判断是否是数字
function isNum(num){
var regu = "^[0-9]*$";
var re = new RegExp(regu);
if (num.search(re) != -1){
return true;
}
else{
return false;
}
}
//根据 Radio 名字取 Radio的值
function getRadioValue(name){
for(var i=1;i<=2;i++){
var radioXElement = document.getElementsByName(name);
for(var j=0;j<radioXElement.length;j++){
if(radioXElement[j].checked==true){
var sex = radioXElement[j].value;
return sex;
}
}
}
}
//Ctrl+Enter触发事件
function isKeyTrigger(e,keyCode){
var argc = isKeyTrigger.arguments.length;
var bCtrl = false;
if(argc > 2){
bCtrl = argv[2];
}
var bAlt = false;
if(argc > 3){
bAlt = argv[3];
}
var nav4 = window.Event ? true : false;
if(typeof e == 'undefined') {
e = event;
}
if( bCtrl &&
!((typeof e.ctrlKey != 'undefined') ?
e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)){
return false;
}
if( bAlt &&
!((typeof e.altKey != 'undefined') ?
e.altKey : e.modifiers & Event.ALT_MASK > 0)){
return false;
}
var whichCode = 0;
if (nav4) whichCode = e.which;
else if (e.type == "keypress" || e.type == "keydown")
whichCode = e.keyCode;
else whichCode = e.button;
return (whichCode == keyCode);
}
function ctrlEnter(e){
var ie =navigator.appName=="Microsoft Internet Explorer"?true:false;
if(ie){
if(event.ctrlKey && window.event.keyCode==13){sendSub();}
}else{
if(isKeyTrigger(e,13,true)){sendSub();}
}
}
//在页面中应用,eg. <textarea id="msg" style="width:100%;height:250px" onkeyup="javascript:return ctrlEnter(event);"></textarea>
//JavaScript执行字符串,即把字符串当代码执行,如下的结果为5
eval("2 + 3");
string.replace(/\s*<\?/g, "<?");//全文替代字符串中的<?准确地替代为 <?
/**
如果想加载完页面后就立即加载javascript函数就可以直接用下面的函数
addLoadEvent(函数名)
*/
function addLoadEvent(func){
var oldonload=window.onload;
if(typeof window.onload!='function'){
window.onload=func;
}else{
window.onload=function(){
oldonload();
func();
}
}
}
//删除一个元素节点或者文本节点,参考http://book.csdn.net/bookfiles/344/10034413523.shtml
var anchor = document.getElementById("sirius");
var parent = anchor.parentNode;
parent.removeChild(anchor);
/**一个删除元素的函数和测试例子*/
<html>
<head>
<title>Remove Element</title>
<script type="text/javascript">
//删除元素节点
function delElement(o){
var ogff=o.parentNode.parentNode.parentNode;
var ogf=o.parentNode.parentNode;
//javascript都是通过父节/父元素点来删除元素或节点的
ogff.removeChild(ogf);
}
</script>
</head>
<body>
<center>
<table>
<tr id="t_tr">
<!-- this表示input元素-->
<td id="t_td">A<input type="button" value="delete" onclick="delElement(this)" /></td>
</tr>
<tr id="t_tr">
<td id="t_td">B<input type="button" value="delete" onclick="delElement(this)" /></td>
</tr>
</table>
</center>
</body>
</html>
/**
insertBefore(targetElement,Element):把Element放到targetElement起前面
为了和javascrpt的insertBefore(targetElement,Element)对应而写的
insertAfter(newElement,targetElement):把newElement放到targetElement后面
*/
function insertAfter(newElement,targetElement){
var parent=targetElement.parentNode;
if(parent.lastChild==targetElement){
parent.appendChild(newElement);
}else{
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
/**
* 字串相关 String
*/
/** 修剪字串前后的空格 */
function trim(s) {
var count = s.length;
var st = 0; // start
var end = count-1; // end
if (s == "") return s;
while (st < count) {
if (s.charAt(st) == " ")
st ++;
else
break;
}
while (end > st) {
if (s.charAt(end) == " ")
end --;
else
break;
}
return s.substring(st,end + 1);
}
/**
* 对Form的多选框进行处理
* mode A-全选,B-反选
*/
function selectAll(box,mode){
var check = box.checked;
var agree=document.getElementById("agree");
var disagree=document.getElementById("disagree");
var boxes = document.getElementsByName("checkbox");
switch (mode){
case 'A': // 全选
disagree.checked=false;
for(var i=0;i<boxes.length;i++){
// alert(boxes[i].checked )
boxes[i].checked = check;
}
break;
case 'B': // 反选
agree.checked=false;
for(var i=0;i<boxes.length;i++){
if(boxes[i].checked==true){
boxes[i].checked=false;
}else{
boxes[i].checked=true
}
}
break;
}
}
全选<input id="agree" type="checkbox" onclick="selectAll(this,'A')"/>
反选<input id="disagree" type="checkbox" onclick="selectAll(this,'B')"/>
//验证时间格式 YYYY-MM-DD
function isDate(date){
var regu = "^[0-9]{4}-([0-1]?)[0-9]{1}-([0-3]?)[0-9]{1}$";
// var regu = "^([0-9]*[.0-9])$"; // 小数测试
var re = new RegExp(regu);
if (date.search(re) != -1)
return true;
else
return false;
}
//这是另外的客户端校验,可以不要
function check_Num()//检查输入是否是数字,不是数字就部响应输入
{
if((event.keyCode<48) || (event.keyCode>57))
return false;
}
//用于判断层的位置,用于把某个元素放在另一个元素的下面
function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
}
return [curleft,curtop];
}
//例如:
//层的定位,这里是把DIV放到t_object元素的下面去
//把document.getElementById("cele_date")的元素放在positionElem元素的下面
var positionName=t_object.getAttribute("id");//t_object在这里是某个元素
var positionElem=document.getElementById(positionName);
var posDiv=findPos(positionElem);
posDiv.push(positionElem.offsetHeight);
posDiv.push(positionElem.offsetWidth)
document.getElementById("cele_date").style.top=(posDiv[1]+posDiv[2])+'px';
document.getElementById("cele_date").style.left=posDiv[0]+'px';
/**
* 按外部XSL格式转换XML文档到页面的div元属中
* divID:div的id
* xmlFileStr:xml文件的内容
* xslFileStr:xsl文件的路径
*/
// 按XSL格式转换XML文档到divID元属中
function transformXml (divID, xmlFileStr, xslFileStr) {
var divObj = document.getElementById(divID);
//alert(divObj)
//alert(xmlFileStr);
if (typeof window.ActiveXObject != 'undefined') { // 支持IE浏览器
xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xslDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.loadXML(xmlFileStr);//xml文件/xml的内容
xslDoc.async = false;
xslDoc.load(xslFileStr);//xsl文件
//alert("*********************"+xslDoc.xml)
//alert(xmlDoc.transformNode(xslDoc));
divObj.innerHTML = xmlDoc.transformNode(xslDoc);
//alert(divObj.innerHTML);//为空字符串
}
else if((typeof document.implementation != 'undefined')
&&(typeof document.implementation.createDocument!='undefined')) { // 支持Mozilla浏览器
xslDoc = document.implementation.createDocument("", "", null);
// 定义XSLTProcessor对象
try{
var dp = new DOMParser();
var xmlDOM = dp.parseFromString(xmlFileStr, "text/xml");
xslDoc.async = false;
xslDoc.load(xslFileStr);
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
// transformToDocument方式
var result = xsltProcessor.transformToDocument(xmlDOM);
var xmls = new XMLSerializer();
//alert(xmls.serializeToString(result));
divObj.innerHTML = xmls.serializeToString(result);
}catch(e){
alert(e.name + ": " + e.message);
return "";;
}
}
}
// 按XSL格式转换XML文档到div元属中
/*
divID元素的id
xmlFileStr:xml文件的路径
xslFileStr:xsl文件的路径
*/
function transformXml (divID,xmlFileStr, xslFileStr) {
var divObj = document.getElementById(divID);
if (typeof window.ActiveXObject != 'undefined') { // 支持IE浏览器
xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xslDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.load(xmlFileStr);//xml文件的路径
xslDoc.async = false;
xslDoc.load(xslFileStr);//xsl文件的路径
divObj.innerHTML = xmlDoc.transformNode(xslDoc);
}
else if((typeof document.implementation != 'undefined')
&&(typeof document.implementation.createDocument!='undefined')) { // 支持Mozilla浏览器
xmlDoc = document.implementation.createDocument("", "", null);
xslDoc = document.implementation.createDocument("", "", null);
// 定义XSLTProcessor对象
xmlDoc.async = false;
xmlDoc.load(xmlFileStr);
xslDoc.async = false;
xslDoc.load(xslFileStr);
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
var resultHtml = xsltProcessor.transformToDocument(xmlDoc);
oXmlSerializer = new XMLSerializer();
divObj.innerHTML = oXmlSerializer.serializeToString(resultHtml);
}
}
//拿取下拉列表所选择的值或其选择项内容
function getChoseValue(flag){//flag是下拉列表的id
var choseArray = document.getElementById(flag);
for(var i=0;i<choseArray.length;i++){
if(choseArray[i].selected == true){
//alert(choseArray[i].text)//选择项的内容,choseArray[i].text等价浴于choseArray.options[i].text
return choseArray[i].value;//值
}
}
document.getElementById("sel").options.length=0;//清空select里的所有option选项,参考http://www.toto369.net/jsjq/s/select/select1.htm
}
//离开当前链接就触发事件,关闭窗口也会触发该事件
<body onbeforeunload="LeaveWin()">
function LeaveWin(){
// do more thing
alert("离开当前链接就触发事件
}
拿取radio的值
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<script type="text/javascript">
function test(){
var testContent = "";
for(var i=1;i<=2;i++){
var radioXElement = document.getElementsByName("x"+i);
for(var j=0;j<radioXElement.length;j++){
if(radioXElement[j].checked==true){
testContent = testContent+radioXElement[j].value;
}
}
}
alert(testContent);
}
</script>
</head>
<body>
<table>
<tr class="tr">
<td></td>
<td class='td' > <input type="radio" name="x1" value="XX1" style="width:13px"/>XX1</td>
<td class='td' > <input type="radio" name="x1" value="YY1" style="width:13px"/>YY1</td>
<td></td>
</tr>
<tr class="tr">
<td></td>
<td class='td' > <input type="radio" name="x2" value="XX2" style="width:13px"/>XX2</td>
<td class='td' > <input type="radio" name="x2" value="YY2" style="width:13px"/>YY2</td>
<td></td>
</tr>
<tr class="tr">
<td></td>
<td colspan="2"><input type="button" value="test" onclick="test()" size="10"/></td>
<td></td>
</tr>
</table>
</body>
</html>
/**
* 用javascript的正则表达式判断输入的格式是否正确
* @param beginDate:输入的日期
*/
function checkDateFormat(beginDate){
beginDate = document.getElementById("beginDate").value;
if (beginDate.length != 10) {
alert("请填写正确的日期格式(YYYY-MM-DD)!");
return false;
}
var reg = /^(?:19|20\d{2})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[0|1])$/;
if (!reg.test(beginDate)) {
alert("请填写正确的日期格式(YYYY-MM-DD)或日期是否正确!");
return false;
}else{
return true;
}
}
//Javascript中Array的默认方法里没有提供indexOf方法,那也自己动手加一个进去
//以prototype方式来Hack Javascript 真的很爽
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(item, i) {
i || (i = 0);
var length = this.length;
if (i < 0) i = length + i;
for (; i < length; i++)
if (this[i] === item) return i;
return -1;
};
}
//javascript没有replaceAll方法,用正则表达写的replaceAll方法
String.prototype.replaceAll = function(s1,s2){//去掉内容空格
return this.replace(new RegExp(s1,"gm"),s2);
}