2012-10-10 21 views
1

我必須在ASP.net頁的文本字段中添加默認的灰色文字是這樣的:新手問題關於JavaScript的聚焦狀態

Image01

而當用戶點擊/進入現場,默認文字消失。

如何在文本框中執行此操作?

我設法實現onFocus事件來更改文本顏色;在我.aspx頁面創建一個<script>標籤的JS代碼:背後

<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標記?謝謝。

+1

什麼是你的問題? – udidu

+1

如果你可以使用jQuery,你會更容易學習它。 http://api.jquery.com/focusin/ –

+0

@udidu請檢查我的更新問題 – AbdulAziz

回答

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/

+0

哇,謝謝Berker。你的小提琴爲我工作,雖然我到現在還沒有使用Jquery,但現在它幫助我。再次感謝您 – AbdulAziz

+1

反芻剛纔添加的只是這一點,而不是:'$(「#文本框」)ATTR(「佔位符」,「在這裏輸入文字...」);' –

+1

@PraveenKumar很好抓,雖然我沒有仔細看xD +1 –

1

是否有可能生成這樣的東西?

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

所以,你需要其實這placeholder="Enter your text here..."屬性添加到<input />標籤。

+0

謝謝你的回答,但我沒有使用任何HTML標記在我的aspx頁面(管理限制) – AbdulAziz

+0

txtBox。 Attributes.Add(「placeholder」,「在這裏輸入你的文字...」) –

+0

請檢查我更新的問題 – AbdulAziz

1

這是你的意思嗎?

<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> 
1

我一無所知ASP.NET,但因爲你可以添加一個聚焦狀態監聽器,你應該能夠做這樣的事情:

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)是一個惱人的界面功能,應該很少使用。

1

我想你要找的水印在文本字段中的效果?

如果是這樣,有幾種方法可以做到這一點。
1)使用AjaxToolKit libraray(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> 

來源:
http://blogs.planetcloud.co.uk/mygreatdiscovery/post/A-simple-jQuery-watermark-textbox.aspx
http://www.misfitgeek.com/2011/03/textbox-input-watermark-using-jquery/