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

js常用操作

2010-01-18 14:31 225 查看
1.禁用按钮和判断text值是否发生改变

js代码如下:

var changing;
function changeBegin()
{
changing = setInterval("onChange()",100);
}
function onChange()
{
if(document.getElementById("Text1").value != document.getElementById("Text2").value)
{
document.getElementById("onOk").disabled = true;
}
else
{
document.getElementById("onOk").disabled = false;
}
}
function changeEnd()
{
clearInterval(changing);
}


HTML部分

<input id="Text1" type="text" name="textfield" runat="server">
<input id="Text2" type="text" name="textfield2" runat="server" onfocus="javascript:changeBegin()"  onblur ="javascript:changeEnd()">
<INPUT type="button" id="onOk" value="Button">


2.js动态更换图片

js代码如下:

var changing;
function changeBegin()
{
changing = setInterval("onChange()",100);
}
function onChange()
{
if(document.getElementById("Text1").value != document.getElementById("Text2").value)
{
document.getElementById("IMG1").src = "images/8_26.jpg";
return false;
}
else
{
document.getElementById("IMG1").src = "images/会员1_08.jpg";
return false;
}
}
function changeEnd()
{
clearInterval(changing);
}


其中return false;一定要加不然ie6不会更改图片,具体什么原因不清楚。。。

3.去除字符串中的空格:

var str = "   hello    ";
var test = str.replace(/(^/s*)|(/s*$/g,"");//去除两边空格
//test = str.replace(/(^/s*)/g,""); //去除左边空格
//test = str.replace(/(/s*$)/g,""); //去除右边空格
alert("#"+test+"#");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: