.preventDefault() 在 on.('input', function{}) 中不起作用
.preventDefault() not working in on.('input', function{})
我想将我的可编辑 div 中的字符数限制为 10 个。在用户达到限制后,我想使用 .preventDefault()
方法来保护 div 免受额外的字符。你能帮帮我吗?
JSFiddle
https://jsfiddle.net/7x2dfgx6/1/
HTML
<div class="profileInfo" id="About" style="border:solid;" contenteditable="true">OOOOO</div>
JQuery
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $('#About').text().length;
rem = limit - char;
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
if(char>limit)
{
event.preventDefault();
//TODO
}
});
});
尝试更改 contenEditable 属性的值。此外,我将限制检查设置为 >=
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $('#About').text().length;
rem = limit - char;
event.preventDefault();
if(char>=limit)
{
$('#About').attr('contenteditable','False');
//TODO
}
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
});
});
使用keydown
代替input
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('keypress', function(event) {
var char = $('#About').text().length;
if(char>limit)
{
return false;
//or
event.preventDefault();
//TODO
}
rem = limit - char;
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
});
});
DEMO
回答你的主要问题:
.preventDefault()
取消 cancelable 事件的行为。 input
事件不是。
要检查 event
是否可取消,请尝试:console.log(event.cancelable);
。
对于 input
,它将显示 "false"
你可能想要这样的东西(未经测试):
const limit = 10;
var rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $(this).text().length;
rem = limit - char;
if(rem <= 0){
$(this).text($(this).text().slice(0, limit));
}
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
});
编辑
JSFiddle demo
由于它是 contentEditable,我不得不使用额外的代码(来自 this 答案)在输入的末尾设置插入符号。
这是一种限制输入字符的方法。适用于粘贴和键入。
/**
* Limit an inputs number of characters
* @param {event} e - event of the input the user is typing into
* @param {number} limit - number of characters to limit to
*/
function limitInputCharacters(e, limit){
if(!e){
return;
}
var inputText = e.target.value;
var charsLeft = limit - inputText.length;
if (charsLeft >= 0) {
// do something, or nothing
} else if (charsLeft < 0) {
e.target.value = inputText.slice(0, limit)
}
}
document.addEventListener("DOMContentLoaded", function() {
const aboutTextArea = document.getElementById('input-about');
aboutTextArea.addEventListener("input", function(e) {
limitInputCharacters(e, 300)
})
})
el.addEventListener('input', function(event) {
if ( this.innerHTML.length >300 && event.keyCode != 8) {
document.execCommand("undo");
}
});
我用 JavaScript 尝试了这个解决方案。
输入下方还有字符计数器。
HTML:
<input type="text" class="about">
<span></span>
javascript:
let about = document.querySelector("input")
let text = document.querySelector("span")
const limit = 10
about.addEventListener("keypress" ,function(event) {
let char = about.value.length;
if(char>=limit){
event.preventDefault();
}
let rem = limit - char
text.innerText = ` You have ${rem} chars left.`
})
我想将我的可编辑 div 中的字符数限制为 10 个。在用户达到限制后,我想使用 .preventDefault()
方法来保护 div 免受额外的字符。你能帮帮我吗?
JSFiddle https://jsfiddle.net/7x2dfgx6/1/
HTML
<div class="profileInfo" id="About" style="border:solid;" contenteditable="true">OOOOO</div>
JQuery
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $('#About').text().length;
rem = limit - char;
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
if(char>limit)
{
event.preventDefault();
//TODO
}
});
});
尝试更改 contenEditable 属性的值。此外,我将限制检查设置为 >=
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $('#About').text().length;
rem = limit - char;
event.preventDefault();
if(char>=limit)
{
$('#About').attr('contenteditable','False');
//TODO
}
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
});
});
使用keydown
代替input
jQuery(document).ready(function($){
const limit = 10;
rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('keypress', function(event) {
var char = $('#About').text().length;
if(char>limit)
{
return false;
//or
event.preventDefault();
//TODO
}
rem = limit - char;
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
});
});
DEMO
回答你的主要问题:
.preventDefault()
取消 cancelable 事件的行为。 input
事件不是。
要检查 event
是否可取消,请尝试:console.log(event.cancelable);
。
对于 input
,它将显示 "false"
你可能想要这样的东西(未经测试):
const limit = 10;
var rem = limit - $('#About').text().length;
$("#counter").append("You have <strong>" + rem + "</strong> chars left.");
$("#About").on('input', function(event) {
var char = $(this).text().length;
rem = limit - char;
if(rem <= 0){
$(this).text($(this).text().slice(0, limit));
}
$("#counter").html("You have <strong>" + rem + "</strong> chars left.");
});
编辑
JSFiddle demo
由于它是 contentEditable,我不得不使用额外的代码(来自 this 答案)在输入的末尾设置插入符号。
这是一种限制输入字符的方法。适用于粘贴和键入。
/**
* Limit an inputs number of characters
* @param {event} e - event of the input the user is typing into
* @param {number} limit - number of characters to limit to
*/
function limitInputCharacters(e, limit){
if(!e){
return;
}
var inputText = e.target.value;
var charsLeft = limit - inputText.length;
if (charsLeft >= 0) {
// do something, or nothing
} else if (charsLeft < 0) {
e.target.value = inputText.slice(0, limit)
}
}
document.addEventListener("DOMContentLoaded", function() {
const aboutTextArea = document.getElementById('input-about');
aboutTextArea.addEventListener("input", function(e) {
limitInputCharacters(e, 300)
})
})
el.addEventListener('input', function(event) {
if ( this.innerHTML.length >300 && event.keyCode != 8) {
document.execCommand("undo");
}
});
我用 JavaScript 尝试了这个解决方案。
输入下方还有字符计数器。
HTML:
<input type="text" class="about">
<span></span>
javascript:
let about = document.querySelector("input")
let text = document.querySelector("span")
const limit = 10
about.addEventListener("keypress" ,function(event) {
let char = about.value.length;
if(char>=limit){
event.preventDefault();
}
let rem = limit - char
text.innerText = ` You have ${rem} chars left.`
})