将jQuery转换为普通JavaScript
如何将此将jQuery转换为普通JavaScript,javascript,jquery,Javascript,Jquery,如何将此jQuery代码转换为普通JavaScript $("#txtTestNumbersOnlyRegex").keyup(function () { var newValue = $(this).val().replace(/[^0-9]/g,''); $(this).val(newValue); }); 简单如下 document.querySelector('#txtTestNumbersOnlyRegex').onkeyup = function() {
jQuery代码
转换为普通JavaScript
$("#txtTestNumbersOnlyRegex").keyup(function () {
var newValue = $(this).val().replace(/[^0-9]/g,'');
$(this).val(newValue);
});
简单如下
document.querySelector('#txtTestNumbersOnlyRegex').onkeyup = function() {
var newValue = this.value.replace(/[^0-9]/g, '');
this.value = newValue;
}
更新:带角度的
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, HelloComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
// app.component.html
<input type="text" [(ngModel)]="value" (keyup)="replacer(value)">
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 4';
text: string;
replacer(value: string) {
this.text = value.replace(/[^0-9]/g, '');
}
}
//app.module.ts
从“@angular/core”导入{NgModule};
从“@angular/platform browser”导入{BrowserModule};
从'@angular/forms'导入{FormsModule};
从“./app.component”导入{AppComponent};
从“./hello.component”导入{HelloComponent};
@NGD模块({
导入:[BrowserModule,FormsModule],
声明:[AppComponent,HelloComponent],
引导:[AppComponent]
})
导出类AppModule{}
//app.component.html
//app.component.ts
从'@angular/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
名称='4';
文本:字符串;
替换程序(值:字符串){
this.text=value.replace(/[^0-9]/g');
}
}
一种可能的Jquery->JS转换
document.getElementById('txtTestNumbersOnlyRegex')。addEventListener(“keyup”,function()){
var newValue=this.value.replace(/[^0-9]/g');
this.value=newValue;
});代码>
转换非常简单:
使用DOM命令侦听元素上的事件:
document.querySelector("#txtTestNumbersOnlyRegex").addEventListener('keyup', function(){
var newValue = this.value.replace(/[^0-9]/g,'');
this.value = newValue;
})
你会如何在angular 2中使用它,谢谢你!!但是我得到了以下错误:error TypeError:_co.replacer不是function@EladNachum您是否检查了示例?是的,我复制了代码,但它与我的代码不兼容:本公司生产的拉德纳库姆你应该先生成角度应用的角度应用的角度应用的角度应用的角度应用的角度应用的情况下,你应该先生成角度应用的角度应用的角度应用的角度应用的情况下,你应该先生成角度应用的角度应用的角度应用的角度应用的情况下,你应该首先与谷歌或者谷歌或者检查像谷歌或者像谷歌这样的网站,或者检查像谷歌或者像谷歌这样的网站,是一个免费的免费代码转换服务或者是免费的免费代码转换服务,是一个免费代码转换服务的免费代码转换服务,本公司14 14 14 14日,14月14月14日,14日,14月14日,14月14日,14日,14月14日,14日,14月14日,14日,本本无无无无电电电电电14日,14日,14日,14日,15日,15日,15日,本本本本本本公司15日,15月15日,15日,15日无无无无托托托托托托在2014年9月月月的现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场ב2014年8月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月月日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日日14日晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚晚$(this).val()
是jQuery