ace*_*ner 1 html javascript regex jquery jsfiddle
我还在学习jQuery,我在网上提到了一些教程.我从这个站点获取了我的代码并将其插入JSFiddle以尝试但它没有工作:
HTML
<input id="mytextbox" style="width:300px" placeholder="Only English letters are allowed here...">
Run Code Online (Sandbox Code Playgroud)
JS
$("#mytextbox").on("keypress", function(event) {
// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
// For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
// Retrieving the key from the char code passed in event.which
// For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
var key = String.fromCharCode(event.which);
//alert(event.keyCode);
// For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
// keyCode == 8 is backspace
// keyCode == 37 is left arrow
// keyCode == 39 is right arrow
// englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
return true;
}
// If we got this far, just return false because a disallowed key was typed.
return false;
});
$('#mytextbox').on("paste",function(e)
{
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
我使用的框架是No-Library(纯JS),并且扩展设置为onload.
但是这个脚本似乎可以在我提到的网站上运行.我可以知道我做错了吗?是不是我没有包含一些jQuery库?
作为一个框架,你需要选择一个jQuery版本(例如jQuery 2.1.0)而不是No-Library(纯JS).
如果不这样做,jQuery库将不会被加载,因此你的jQuery相关代码将无法工作.