简体   繁体   English

如何更改jQuery中的密码字段类型?

[英]How to change the password field type in jquery?

I am trying to change the type of password field (change from password to text).The issue is that clicked Item (original example there is eye pic but the current demo I place a text see ) is present in after of the element. 我想(从密码更改为文本)更改密码字段的类型.The问题是,点击项目(例如原来有eye PIC但目前的演示中,我把文本see )是目前在after的元素。

Could I bind the after element? 我可以绑定after元素吗? I am sure 100% we can't bind after element. 我敢肯定100%我们不能after元素after绑定。 So how will I toggle my password? 那么,如何切换密码?

Here is my code https://jsbin.com/zeterucite/2/edit?html,css,js,output 这是我的代码https://jsbin.com/zeterucite/2/edit?html,css,js,输出

$(function(){
  function signUpAgreeCheckedEventHandler(e) {
        e.preventDefault();
        e.stopPropagation();
    $('#signupConfirmPassword_js').attr('type','text')
        alert('--')
    }
  $('#signupConfirmPassword_js:after').click(signUpAgreeCheckedEventHandler)
})

As mentioned in comments, it is not possible to get the :after pseudo element by java script. 如评论中所述,不可能通过Java脚本获取:after伪元素。 but there are some workarounds for it like as applying pointer-events: none; 但是有一些解决方法,例如应用pointer-events: none; on your main element and a pointer-events: all; 在您的主要元素和一个pointer-events: all; on its pseudo element as you use in your demo code. 在演示代码中使用它的伪元素。

Another option is to add the click event handler for whole main element which includes the :after pseudo object and then use some xy calculations to determine if the click is over the main part or your pseudo element. 另一种选择是为整个主要元素添加click事件处理程序,包括:after伪对象,然后使用一些xy计算确定点击是否在主要部分或伪元素上。

$('#myPassword').click( function(evt) {
  if (evt.clientX > $(this).offset().left + 100 { //assume input element width is 100px
    // ...
  }
});

You may use e.clientX, e.clientY, e.pageX/Y, $(this).offset().top/left, $(this).outerWidth/outerHeight ... 您可以使用e.clientX, e.clientY, e.pageX/Y, $(this).offset().top/left, $(this).outerWidth/outerHeight ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM