我正在编写一个 JavaScript 库。这是基本代码:
(function (window) {
var versrionInfo = {
release : 1.0,
date : "10/5/2013",
releaseNotes : "This is the first oficial release of techx. Basic functions have been implemented."
},
regex = {
Id : /^[#]\w+$/,
Class : /^[.]\w+$/,
Tag : /^\w+$/,
validSelector : /^([#]\w+|[.]\w+|\w+)$/
},
tex = function(selector){
//only some of the functions need to select an element
//EX:
// style: tex(selector).style(style);
//one that would not need a selector is the random number function:
// tex().random(from,to);
if (selector){
if (typeof selector === 'string'){
var valid = regex.validSelector.test(selector);
if( valid ){
this.length = 1;
if(regex.Id.test(selector)){
this[0] = document.getElementById(selector);
}
if(regex.Class.test(selector)){
this[0] = document.getElementByClass(selector);
}
if(regex.Tag.test(selector)){
this[0] = document.getElementByTagName(selector);
}
}
}else if(typeof selector === 'object'){
this = selector;
}
//this = document.querySelector(selector);
// I could make a selector engine byt I only need basic css selectors.
}
};
tex.prototype = {
dit : function(){
this.innerHTML = 'Hi?!?!?!';
}
};
window.tex = tex;
})(window);
在正文部分,我有一个输入和一个带有 id 的 div test
。在输入按钮上,我有这个:onclick=tex('#test').dit();
。当我尝试运行代码时,我收到一个错误,指出它未定义。有谁知道我的代码有什么问题?