这段代码会起作用吗? Javascript / asp.net(代码无效)

时间:2009-12-23 11:56:45

标签: asp.net javascript

        <td class="style4">
            <input type="text" ID="txtFirstName" runat="server" maxlength="50"
            class="DefaultTextbox" style="width:180px;" value="First Name" 
            onfocus="if(this.value=='First Name') {this.value = '';document.getElementById('spanFirstName').visible=false;}"
            onblur="if(this.value=='') this.value = 'First Name'"
            />
        </td>
        <td>
             <span id="spanFirstName">Should be less than 50 characters.</span>
        </td>

我想做这样的事情

onfocus = "if(this.value=='First Name') 
{
    this.value = '';
    document.getElementById('spanFirstName').visible=false;
}"
onblur = "if(this.value=='') this.value = 'First Name'"

2 个答案:

答案 0 :(得分:1)

我不知道asp.net但是在看完你的代码之后:

document.getElementById('spanFirstName').visible=false;

我认为它可以替换为以下行:

document.getElementById('spanFirstName').style.visibility='hidden';

不确定,但这就是我们这样做的原因,这可能是问题的原因。

由于

答案 1 :(得分:1)

我认为您正在寻找 TextBoxWatermark 。这是一个很好的

<强> Create Textbox Watermark Using CSS and JavaScript

对于您的代码,您可以这样做

<script type='text/javascript'>
function HideSpan(elem)
{
    if ( elem.value == 'First Name' )
    {
        elem.value = '';
        document.getElementById("spanFirstName").style.visibility = 'hidden';   
    }   
}

function SetText(elem)
{
    if ( elem.value == '' )
    {
        elem.value = 'First Name';
            document.getElementById("spanFirstName").style.visibility = 'visible';
    }   
}

</script>
<table>
<tr>
      <td class="style4">
            <input type="text" ID="txtFirstName" runat="server" maxlength="50"
            class="DefaultTextbox" style="width:180px;" value="First Name" 
            onfocus="HideSpan(this);" onblur="SetText(this);"
            />
        </td>
        <td>
             <span id="spanFirstName">Should be less than 50 characters.</span>
        </td>
</tr>
</table>