[英]Replacing or Adding an onfocus & onblur function to a textbox within a jQuery function
I have a textbox which has a text value and onfocus and onblur functions attached which make the text disappear and appear when clicking on or off it... 我有一个带有文本值的文本框,并附加了onfocus和onblur函数,这些函数使文本消失,并在单击或关闭时显示...
The functions for onblur and onfocus attached to the textbox is... 附加到文本框的onblur和onfocus功能是...
function feedbackTextBoxFocus(textFieldObject, initialText) {
if (textFieldObject) {
if (textFieldObject.value == initialText)
textFieldObject.value = '';
}
}
function feedbackTextBoxBlur(textFieldObject, initialText) {
if (textFieldObject) {
if (textFieldObject.value == '')
textFieldObject.value = initialText;
}
}
The textbox is... 文本框是...
<asp:TextBox ID="TxtAskQuestionTitle" runat="server" CssClass="KBTopQuestionTitleTextbox" Text="Ask a question on this topic..." onblur="feedbackTextBoxBlur(this,'Ask a question on this topic...')" onfocus="feedbackTextBoxFocus(this,'Ask a question on this topic...')" />
What i want to do via a jquery method i have which makes a content area slide down when the textbox has been clicked on, is to change the text within the textbox and have the onblur and onfocus values change to "Provide a title for your question..." 我想通过jquery方法执行的操作是在单击文本框时使内容区域向下滑动,它是更改文本框内的文本,并将onblur和onfocus值更改为“为您的问题提供标题” ......”
$(function () {
$('.QuestionContentExpandClick').click(function () {
/**** Slide the content div ****/
var viewContent = $('#DivExpandQuestionContentArea');
viewContent.slideDown('slow', function () {
// If logged in and sliding the content area down
// the title box message changes
var titleTextbox = $('#TxtAskQuestionTitle');
titleTextbox.val('Provide a title for your question...');
//titleTextbox.attr('onfocus', feedbackTextBoxFocus(this,'Provide a title for your question...'));
//titleTextbox.attr('onblur', feedbackTextBoxBlur(this, 'Provide a title for your question...'));
});
});
});
As you can see i am able to change the text value within the textbox but cannot find a way to change the onblur and onfocus functions, can anyone help me with this? 如您所见,我能够在文本框中更改文本值,但找不到改变onblur和onfocus函数的方法,有人可以帮助我吗? Many thanks in advance! 提前谢谢了!
If you use parenthesis when putting a callback function, you are not passing reference, instead you are executing the function. 如果在放置回调函数时使用括号,则您没有传递引用,而是执行了该函数。 Also, you just want to use the .focus()
and .blur()
functions. 另外,您只想使用.focus()
和.blur()
函数。 Try like so: 尝试这样:
titleTextbox.focus(function() {
feedbackTextBoxFocus(this,'Provide a title for your question...');
});
titleTextbox.blur(function() {
feedbackTextBoxBlur(this, 'Provide a title for your question...');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.