【问题标题】:Newbie Issue About JavaScript OnFocus关于 JavaScript OnFocus 的新手问题
【发布时间】:2012-09-30 21:19:01
【问题描述】:

我必须在 ASP.net 页面的文本字段中添加默认灰色文本,如下所示:

当用户点击/输入一个字段时,默认文本消失。

如何在文本框事件中执行此操作?

我设法实现了 onFocus 事件来改变文本颜色;在我的 .aspx 页面中,我为 JS 代码创建了一个 <script> 标记:

<script type="text/javascript">
    function test(obj) {
        obj.style.color = "grey";
    }
</script>

后面的代码:

txtBox.Attributes.Add("OnFocus", "test(this)")
'txtBox is the ID of text Box

现在问关于 JavaScript OnFocus 事件的非常基本的问题很尴尬。

但是问题是知识的关键 :)

编辑:我不能在我的 ASP.Net 页面中使用任何 HTML 标记

有什么帮助吗?谢谢。

【问题讨论】:

  • 如果您可以使用 jQuery,那么您学习它会容易得多。 api.jquery.com/focusin
  • @udidu 请检查我更新的问题

标签: javascript asp.net vb.net onfocus


【解决方案1】:

我猜您正在寻找文本字段中的 水印 效果?

如果是这样,有几种方法可以做到这一点。

1) 使用AjaxToolKitlibraray (Watermark Effect Demo Here)
2) 使用 HTML + CSS + jQuery

<input type="text" id="text1" value="some text"/>
<script type="text/javascript">
  $(document).ready(function() {
    var tbval = $('#text1').val();
    $('#text1').focus(function() { $(this).val('');});
    $('#text1').blur(function() { $(this).val(tbval);});
  });
</script>

来源:

【讨论】:

    【解决方案2】:

    尝试使用 jQuery

    如何实现jQuery:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    

    html:

    <input type="text" />
    

    css:

    .grey { color:#aaa; }
    

    jQuery:

    var inputval = "";
    $(document).ready(function() {
        // Define your default value to show on input
        $("input[type='text']").val("Enter your text here...");
        // Add grey color or optionally blur effect
        $("input[type='text']").addClass('grey');
        // Save your default value
        inputval = $("input[type='text']").val();
        $("input[type='text']").focusin(function() {
        // if textbox is empty or got the default value
        if ($(this).val() == "" || $(this).val() == inputval) {
            // Clear the box
            $(this).val("");
            // Remove grey color
            $(this).removeClass('grey'); }
        }).focusout(function() {
            // if textbox is empty
            if ($(this).val() == "") {
                // Put your Default value back
                $(this).val(inputval);
                // Add grey color
                $(this).addClass('grey'); }
        });
    });
    

    jsfiddle:http://jsfiddle.net/BerkerYuceer/SWc6g/

    这实际上是一个非常糟糕的编码,但它应该让你了解它是如何工作的。

    编辑:这里是更高效的版本

    html:

    <input type="text" />
    

    jQuery:

    $(document).ready(function() {
        Watermark("input[type='text']","Enter your text here...");
    });
    
    function Watermark(element, WatermarkString) {
        $(element).val(WatermarkString).css('color','#aaa');
        $(element).focusin(function() {
            if ($(this).val() == "" || $(this).val() == WatermarkString) {
                $(this).val("").css('color','#000'); }
        }).focusout(function() {
            if ($(this).val() == "") {
                $(this).val(WatermarkString).css('color','#aaa'); }
        });
    };
    

    jsfiddle:http://jsfiddle.net/BerkerYuceer/vLJ2U/

    【讨论】:

    • 哇,谢谢伯克。你的小提琴对我有用,虽然我直到现在才使用 Jquery,但现在它对我有帮助。再次感谢
    • 你刚刚添加了这个,而不是:$("#textbox").attr("placeholder", "Enter text here...");
    • @PraveenKumar 不错,虽然我没仔细看 xD +1
    【解决方案3】:

    我对 ASP.NET 一无所知,但由于您可以添加 onfocus 侦听器,您应该能够执行以下操作:

    txtBox.Attributes.Add("Value", "Enter your text here...")
    txtBox.Attributes.Add("OnFocus", "updateValue(this)")
    txtBox.Attributes.Add("OnBlur", "updateValue(this)")
    

    updateValue 函数在哪里:

    function updateValue(el) {
        if (el.value == el.defaultValue) {
          el.value = '';
        } else {
          el.value = el.defaultValue;
        }
    }
    

    上面模仿了占位符属性,这(IMO)是一个烦人的界面功能,应该很少使用。

    【讨论】:

      【解决方案4】:

      这是你的本意吗?

      <html>
      <head></head>
      <body>
          <input id="serachbox" type="text" onFocus="initText(this);" />
      
          <script type="text/javascript">
      
              var defaultText = 'Enter your serach here...'
              var initText = function(el){
                  if(el.value == defaultText){
                      el.value = "";
                  }
              }
      
              var input = document.getElementById('serachbox');
      
              input.value = defaultText;
          </script>
      
      </body>
      </html>
      

      【讨论】:

        【解决方案5】:

        有可能生成这样的东西吗?

        <input type="text" placeholder="Enter your text here..." />
        

        所以你实际上需要将这个placeholder="Enter your text here..." 属性添加到&lt;input /&gt; 标记中。

        【讨论】:

        • 感谢您的回答,但我的aspx页面中不必使用任何HTML标签(管理限制)
        • txtBox.Attributes.Add("placeholder", "Enter your text here...")
        • 请查看我的更新问题
        猜你喜欢
        • 1970-01-01
        • 2011-02-01
        • 2011-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 2011-11-11
        • 1970-01-01
        相关资源
        最近更新 更多