javascript - 将多个实例的JS转换为jQuery click函数

标签 javascript jquery

我有一个在Javascript中运行一次的函数(通过调用id)。我尝试将功能转换为jQuery,以允许该功能适用​​于pwd的每个实例。

我如何让jQuery代码像JS代码一样工作,但要在pwd的每个实例上运行?

$(".pass").on("click", ".typcn-eye", function() {
  var pwd = $(this).closest(".pwd");

  function togglePass() {
    $(this).toggleClass("active");

    pwd.type == "password" ? (pwd.type = "text") : (pwd.type = "password");
  }
});




var pwd = document.getElementById('pwd');
var eye = document.getElementById('eye');

eye.addEventListener('click', togglePass);

function togglePass() {

  eye.classList.toggle('active');

  (pwd.type == 'password') ? pwd.type = 'text': pwd.type = 'password';
}

.box {
  display: flex;
  flex-direction: column;
  align-items: start;
}

.pass {
  display: flex;
  align-items: center;
  border: 1px solid;
}

.box input[type="text"],
.box input[type="password"] {
  display: block;
  margin: 20px auto;
  background: #ececec;
  border: 0;
  border-radius: 5px;
  padding: 14px 10px;
  width: 220px;
  outline: none;
  color: #565f79;
}

.typcn {
  color: #3b476b;
  font-size: 22px;
  cursor: pointer;
}

.typcn.active {
  color: #7f60eb;
}

<link href="https://cdnjs.cloudflare.com/ajax/libs/typicons/2.0.9/typicons.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="box">
  <div class="pass">
    <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd" autocomplete="off">
    <i class="typcn typcn-eye" id="eye"></i>
  </div>
  <div class="pass">
    <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd2" autocomplete="off">
    <i class="typcn typcn-eye" id="eye2"></i>
  </div>
</form>

最佳答案

在click事件的jQuery版本上,您具有功能togglePass(),但从未触发过它。由于您使用的是其他版本。



// For triggering you function
$(".pass").on("click", ".typcn-eye", togglePass);

// Function outside your click event
function togglePass() {
    // Toggle active class on every click
    $(this).toggleClass("active");
    // Get the type of the prev element 
    let type = $(this).prev().attr("type"),
        changeTo = type === "password" ? "text" : "password";

    // Change type attr of that element
    $(this).prev().attr("type", changeTo);
}

.box {
    display: flex;
    flex-direction: column;
    align-items: start;
}

.pass {
    display: flex;
    align-items: center;
    border: 1px solid;
}

.box input[type="text"],
.box input[type="password"] {
    display: block;
    margin: 20px auto;
    background: #ececec;
    border: 0;
    border-radius: 5px;
    padding: 14px 10px;
    width: 220px;
    outline: none;
    color: #565f79;
}

.typcn {
    color: #3b476b;
    font-size: 22px;
    cursor: pointer;
}

.typcn.active {
    color: #7f60eb;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/typicons/2.0.9/typicons.min.css" rel="stylesheet" />
<form class="box">
    <div class="pass">
        <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd" autocomplete="off">
        <i class="typcn typcn-eye" id="eye"></i>
    </div>
    <div class="pass">
        <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd2" autocomplete="off">
        <i class="typcn typcn-eye" id="eye2"></i>
    </div>
</form>

关于javascript - 将多个实例的JS转换为jQuery click函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54205473/

相关文章:

javascript - jQuery在任何浏览器中都无法在此页面上使用。我不知道为什么!

javascript - 不正确的 jQuery 元素宽度计算

javascript - Summernote Editor中如何添加Alignment(left Align, center align, right align)工具栏

javascript - 将值隐藏为 Int

javascript - 直接使用对象

javascript - 试图用css display:none隐藏Div元素。没有隐藏它

javascript - 错误消息是指文件内容不存在的文件

javascript - 无法在表分隔符的 ul 之前插入链接

javascript - Ember.js 2.2 - 如何从组件更新路由模型?

javascript - 当用户在上方的输入div中键入文本并使其更高时,可缩短div的高度