vue el-input-number千分符指令

2023-07-27  本文已影响0人  辉色星空下

聚焦时正常显示,失焦时千分符显示

Vue.directive("thousand", {
  // 被绑定元素插入父节点时调用
  inserted: function(el) {
    // 获取input节点
    if (el.tagName.toLocaleUpperCase() !== "INPUT") {
      el = el.getElementsByTagName("input")[0];
    }
    // 千分位
    el.value = parseFloat(el.value).toLocaleString("zh", {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2
    });
    // 聚焦转化为数字格式(去除千分位)
    el.onfocus = e => {
      let a = el.value.replace(/,/g, ""); //去除千分号的','
      el.value = parseFloat(a).toFixed(2);
    };
    el.onblur = e => {
      el.value = parseFloat(el.value).toLocaleString("zh", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      });
    };
  }
});
上一篇下一篇

猜你喜欢

热点阅读