Javascript jQuery将文本放在元素旁边 函数popupMessage(元素,stringVal){ 变量位置=$(元素).position(); 如果(stringVal==“用户名”){ $(元素).text(“左:“+position.left+”,顶部:“+position.top”); }else if(stringVal==“密码”){ }否则{ } }

Javascript jQuery将文本放在元素旁边 函数popupMessage(元素,stringVal){ 变量位置=$(元素).position(); 如果(stringVal==“用户名”){ $(元素).text(“左:“+position.left+”,顶部:“+position.top”); }else if(stringVal==“密码”){ }否则{ } },javascript,jquery,jsf,Javascript,Jquery,Jsf,我正在尝试使用此代码在输入字段旁边放置一条消息。上面可以看到元素和我的脚本。我想要的是基于输入类型,无论是用户名还是密码,我希望在文本字段旁边显示一条消息,告诉人们输入什么。我计划在输入端设置一个onblur事件,让消息消失 我可以在console视图中的元素中显示文本,但在页面上没有显示任何内容。如何正确定位此消息?(“注意,消息现在只是调用元素的位置”) 谢谢 <h:inputText id="usernameInput" value="#{userBean.un}"

我正在尝试使用此代码在输入字段旁边放置一条消息。上面可以看到元素和我的脚本。我想要的是基于输入类型,无论是用户名还是密码,我希望在文本字段旁边显示一条消息,告诉人们输入什么。我计划在输入端设置一个
onblur
事件,让消息消失

我可以在console视图中的元素中显示文本,但在页面上没有显示任何内容。如何正确定位此消息?(“注意,消息现在只是调用元素的位置”)

谢谢

<h:inputText id="usernameInput" value="#{userBean.un}"
                styleClass="form-control" required="true"
                onfocus="popupMessage(this, 'username');">
                <f:validateRegex pattern="([a-zA-Z_0-9]+)"></f:validateRegex>
            </h:inputText>

function popupMessage(element, stringVal) {
                var position = $(element).position();
                if(stringVal === "username"){
                    $( element ).text( "left: " + position.left + ", top: " + position.top ) ;
                }else if(stringVal === "password"){

                }else{

                }
            }
text(string)方法有助于在匹配的选择器上显示文本,但不用于定位文本

-To positon text message beside input text field. You have to use the Jquery .css() method. 

-The .position() method allows us to retrieve the current position of an element relative to the offset parent. Contrast this with .offset(), which retrieves the current position relative to the document. You have to use the right method which helps you.

既然您使用的是Primefaces,为什么不使用组件呢?使用Primefaces工具提示,您可以轻松实现所要实现的目标

$(element).css({
                "left" :position.left
                ,"top" :position.top
              });