我试图在文本表单域上覆盖文本。当焦点事件发生时,文本应该出现在文本框的上方(并且存在错觉),并且留在那里直到用户实际键入一个字母。在焦点发生之前,已经有默认文本。动态表单域与JQuery覆盖
有一个类似/更简单的变化,其中框以默认值开始,默认值在焦点上消失,但这不是我在这种情况下要做的。
我遇到的问题是帮助文本在插入后出现在正确的位置,特别是动态设置顶部和左侧CSS属性。
我的onfocus功能:
TrueThis.OldValue = $('#MyField').val(); // Save the current value
$('#MyField').val(''); // Empty the text box
var OverlayHTML = '<div>This is helper text that goes on top.</div>'
// Insert the Overlay and position the top and left to match the parent
// PROBLEM:: The element is inserted, but is not positioned.
// No top and left css attributes were assigned according to Firebug.
$('#MyField').after(OverlayHTML).css({
'top':$('#MyField').position.top,
'left':$('#MyField').position.left
});
// Remove the overlay when a user starts typing.
$('#MyField').next().keyup(function(){this.remove();});
我认为问题是,我没能抓住新的元素,或者可能直到整个函数完成它不会在DOM存在了吗?