[英]trying to get focus to end of textarea after creating a dynamic value with jquery
[英]jquery textarea value focus
對我來說這可能是愚蠢的,但是我在哪里出錯呢?
$(function() {
$('textarea#comment').each(function() {
var $txt = $(this).val();
$(this).bind('focus', function() {
if($(this).val($txt)) {
$(this).val('');
}
});
});
});
基本上,我希望檢查這種情況下的值是否為默認值“輸入您的評論”,然后將其更改為無。
有人知道怎么做嗎? 我猜它相對簡單。 該代碼目前正在執行的操作是刪除任何值。
好的,首先,您應該只有一個帶有注釋ID的標簽,因為ID應該是唯一的,但是如果我們使用您的代碼,則必須首先設置一個默認值,如下所示:
<textarea id="comment">Enter text here</textarea>
現在的javascript:
(function() {
$('textarea#comment').each(function() {
var $txt = $(this).val();
//alert($txt);
$(this).bind('focus', function() {
if($(this).val() === this.defaultValue) {
$(this).val('');
}
});
});
})();
自執行函數語法已在此處得到糾正,應如下所示:
(function () { // my code here })();
對於默認值,請使用:
this.defaulValue; // works for input boxes as well
希望能幫助到你。 干杯
jquery_example插件完全可以滿足您的需求。 您可以查看其源代碼並獲得一些啟發。 或者,您可以將默認值存儲為標簽上的元數據,並在textarea標簽的change事件中對其進行檢查。
嘗試
$('#comment').live('click change focus', function() {
var $txt = $(this).val();
if($txt == 'Enter your comment' );
$(this).val('');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.