role.name:为原始值
textField :是input框中的值

var textField = document.getElementById("inputName"),//获取表单域
startText = textField.value; //获取开头字符串
textField.onkeyup = function () {
//如果不是以role.name开头的,就把文本框内的值设为role.name
(textField.value.indexOf(role.name) === 0) || (textField.value = role.name);
};

总结就是:在 Input 获得焦点时focus触发函数,利用indexOf方法判断原始值也就是不可更改的值是否存在,不存在就赋值原始的。原理就是这样,别的框架什么的可以根据这个思路来实现。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-06-05
  • 2021-06-28
  • 2022-12-23
  • 2022-02-03
  • 2021-05-18
猜你喜欢
  • 2022-02-09
  • 2021-05-31
  • 2021-10-21
  • 2022-12-23
  • 2021-12-26
  • 2021-10-04
  • 2022-12-23
相关资源
相似解决方案