Javascript 如何验证contenteditable=true的html标记?
例如,在输入标记中,我们有一个名为type的字段,如果我们在其中输入type=“numeric”,则除了数字之外,不允许输入任何内容 如果我将td内容设置为可编辑,我如何防止用户在该td标签中输入除数字以外的任何内容 试试这个Javascript 如何验证contenteditable=true的html标记?,javascript,html,angular,Javascript,Html,Angular,例如,在输入标记中,我们有一个名为type的字段,如果我们在其中输入type=“numeric”,则除了数字之外,不允许输入任何内容 如果我将td内容设置为可编辑,我如何防止用户在该td标签中输入除数字以外的任何内容 试试这个 $('#text').keypress(function(e) { if (!(e.which >= 48 && e.which <= 57)) { return false; } }); $('#text')。按键(功能(e
$('#text').keypress(function(e) {
if (!(e.which >= 48 && e.which <= 57)) {
return false;
}
});
$('#text')。按键(功能(e){
如果(!(e.which>=48&&e.which=48&&e.which试试这个
$('#text').keypress(function(e) {
if (!(e.which >= 48 && e.which <= 57)) {
return false;
}
});
$('#text')。按键(功能(e){
如果(!(e.which>=48&&e.which=48&&e.which,您可以在jQuery.cleaner和简明中使用.keydown()
方法进行尝试。请参阅下面的代码片段
$(“td”)。打开(“按键”,函数(事件){
if(isNaN(String.fromCharCode(event.which))){
event.preventDefault();
}
})
td{
填充:3倍;
边框:1px纯红;
字号:18px;
线高:24px;
宽度:100px;
高度:50px;
}
您可以尝试使用jQuery中的.keydown()
方法。更简洁明了。请参阅下面的代码片段
$(“td”)。打开(“按键”,函数(事件){
if(isNaN(String.fromCharCode(event.which))){
event.preventDefault();
}
})
td{
填充:3倍;
边框:1px纯红;
字号:18px;
线高:24px;
宽度:100px;
高度:50px;
}
此处包含了角度标记
这方面的一个重要原因可能是:
src/onlyNumeric.directive.ts
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({ selector: '[onlyNumeric]' })
export class OnlyNumericDirective {
@HostListener('input') onContentChange() {
this.el.nativeElement.innerText = this.el.nativeElement.innerText.replace(/\D/g,'')
}
constructor(private el: ElementRef) {
}
}
此处已包含角度标记
这方面的一个重要原因可能是:
src/onlyNumeric.directive.ts
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({ selector: '[onlyNumeric]' })
export class OnlyNumericDirective {
@HostListener('input') onContentChange() {
this.el.nativeElement.innerText = this.el.nativeElement.innerText.replace(/\D/g,'')
}
constructor(private el: ElementRef) {
}
}
使用html5模式属性pattern=“\d*”模式仅用于输入元素,对吗?至少链接中是这么说的使用html5模式属性pattern=“\d*”模式只用于输入元素,对吗?至少它在链接中是这么说的。我会用html5模式属性替换onkeypress。这很费劲,但是你能提供一个不使用输入标记的解决方案吗?因为输入标记使它看起来像一个文本框,我不想要that@geethu何塞,这似乎很好用jquery然而,在angular中,这个确切的功能的类比是什么?我会用html5模式属性替换onkeypress,这付出了很多努力,但是你能提供一个不使用输入标记的解决方案吗,因为输入标记使它看起来像一个文本框,我不想要that@geethujose这似乎与jquery配合得很好然而,在angular中,这个确切功能的类比是什么?非常感谢您的快速回答,但我想知道是否存在这样的工作环境是angular?对于angular,您可能想检查Tom给您的答案:)非常感谢您的快速回答,但我想知道这样的工作环境是否存在?对于angular,您可能需要检查Tom给您的答案:)