聚焦時(shí)正常顯示,失焦時(shí)千分符顯示
Vue.directive("thousand", {
// 被綁定元素插入父節(jié)點(diǎn)時(shí)調(diào)用
inserted: function(el) {
// 獲取input節(jié)點(diǎn)
if (el.tagName.toLocaleUpperCase() !== "INPUT") {
el = el.getElementsByTagName("input")[0];
}
// 千分位
el.value = parseFloat(el.value).toLocaleString("zh", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
// 聚焦轉(zhuǎn)化為數(shù)字格式(去除千分位)
el.onfocus = e => {
let a = el.value.replace(/,/g, ""); //去除千分號(hào)的','
el.value = parseFloat(a).toFixed(2);
};
el.onblur = e => {
el.value = parseFloat(el.value).toLocaleString("zh", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
};
}
});