1

在下面的代码示例中,我将window.status“a”替换为“b”

function alternateViaIntrvl() {
    setInterval('alterStatus()', 500);
}
var altrCounter = 0;

function alerted() {
    var txt = "a";
    if (altrCounter % 2 == 0) {
        txt = "b"
    }

    window.status=txta;
    countalerted++;
}

我只是想制作一个从“插入日期”到“mm/dd/yyyy”的备用 Ajax 水印,但是使用 javascript 访问 ajax 太复杂了

set_Text("mm/dd/yyyy")

然后我找到了 ajax 的替代方法:Md.Asaduzzaman AzadjavascriptWaterMark

我试图setInterval()在那个代码上实现一个,

所以我将能够拥有一个整洁的双模式 JavaScript-Watermark。

我知道它必须像window.status备用代码示例一样简单,但我自己不知道如何实现它。

JavaScript

 function Focus(objname, waterMarkText) {
        obj = document.getElementById(objname);
        if (obj.value == waterMarkText) {
            obj.value = "";
            obj.className = "NormalTextBox";
            if (obj.value == "insert date" || obj.value == "" || obj.value == null) {
                obj.style.color = "black";
            }
        }
    }



 function Blur(objname, waterMarkText) {
        var alternateWM1 = "insert date";
        var alternateWM2 = "mm/dd/yyyy";
        count++;
        obj = document.getElementById(objname);
        if (obj.value == "") {
            obj.value = waterMarkText;
            if (objname != "txtPwd") {
                obj.className = "WaterMarkedTextBox";
            }
            else {
                obj.className = "WaterMarkedTextBoxPSW";
            }
        }
        else {
            obj.className = "NormalTextBox";
        }



 if (obj.value == "insert date" || obj.value == "" || obj.value == null) {
    obj.style.color = "gray";
    }

}
  • html

    <table>
        <tr>
            <td>
                User Id
            </td>
            <td>
                <asp:TextBox ID="txtUserId" runat="server" 
            onfocus="Focus(this.id,'insert date')"
                onblur="Blur(this.id,'insert date')" 
                Width="126px" CssClass="WaterMarkedTextBox">
                insert date
               </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <asp:TextBox ID="txtPwd" TextMode="Password" runat="server" 
                        onfocus="Focus(this.id,'')"
                    onblur="Blur(this.id,'')" Width="126px" 
                      CssClass="WaterMarkedTextBoxPSW" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" />
            </td>
        </tr>
    </table>
    </form>

这实际上是一个隐藏的表格行,默认状态是隐藏的,然后在 imageButton 单击事件(代码隐藏)上它变得可见

   TRinsertForm.Style.Add("visibility", "visible");

所以我想我可以在后面运行一个脚本表单代码,它会生成 javascript 代码来设置文本框初始文本或其他东西。

4

1 回答 1

0

这就是我想出的:

请参阅 html 代码,以便将其设置为完美的交替水印,因为我可以使用 javascript 和 ajax 方法结合起来

var original = "insert date";
var alternate = "mm/dd/yyyy";
var masked = "__/__/____";
var countalerted = 0;
var txt = "";
var intird = 0;
if (intird == 0) {
    intrId = setInterval(function () { alterWaterMarkForDateTBX() }, 1000);
}
function SetIntervalTBX() {
    setInterval(function () { alterWaterMarkForDateTBX() }, 1000);
}

function alterWaterMarkForDateTBX() {
    var d = new Date();
    var t = d.toLocaleTimeString();

    if (countalerted < 4) {
        if (countalerted % 2 == 0) {
            document.forms["form1"].elements["TBXinsertDate"].value = original;
        }
        else {
            document.forms["form1"].elements["TBXinsertDate"].value = alternate;
        }
        countalerted++;
    }
    else {
        window.status = intrId;
        clearInterval(intrId);
//        document.forms["form1"].elements["TBXinsertDate"].value = original;
    }
}




var wasFocused = false;var wasBlur = false;
function Focus(objname, waterMarkText) {
    wasFocused = true;
    if (wasBlur == false) {
        obj = document.getElementById(objname);
        if (obj.value == masked) {
            obj.value = ""; obj.removeAttribute("title");
            obj.className = "NormalTextBox";
            if (obj.value == original || obj.value == alternate || obj.value == "" || obj.value == null) {
                obj.style.color = "black";
            }
        }
    }
    else 
    {


            obj.value = "";

    }
}


function Blur(objname, waterMarkText) {


    var alternateWM1 = "insert date";
    var alternateWM2 = "mm/dd/yyyy";
    obj = document.getElementById(objname);

    if (obj.value == "" || obj.value == masked) {
        obj.value = waterMarkText;
        obj.className = "WaterMarkedTextBox";
        wasBlur = true;
    }
    else {
        obj.className = "NormalTextBox";
    }
    if (wasBlur == false) {
        if (obj.value == original || obj.value == alternate || obj.value == "" || obj.value == null ) {
            obj.style.color = "black";
            wasBlur = true;
            if (wasFocused == true) {
                SetIntervalTBX();
            }
        }
    }
}



         <asp:TextBox ID="TBXinsertDate" runat="server" ToolTip="insert date" 
        CssClass="WaterMarkedTextBox" Width="75px" OnTextChanged="TBXinsertDate_TextChanged" 
                 onfocus="Focus(this.id, this.value)"
                 onblur="Blur(this.id, this.value)" >
         </asp:TextBox>

         <cc1:TextBoxWatermarkExtender ID="insertDate_TextBoxWatermarkExtender" runat="server" TargetControlID="TBXinsertDate" WatermarkText="insert date" WatermarkCssClass="WaterMarkedTextBox">
                </cc1:TextBoxWatermarkExtender>
于 2012-10-03T20:49:35.433 回答