[英]Javascript library undefined
I am writing a javascript library. 我正在写一个JavaScript库。 here is the basic code:
这是基本代码:
(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);
In the body section I have an input and a div with the id test
. 在主体部分,我有一个输入和一个带id
test
的div。 On the input button I have this: onclick=tex('#test').dit();
在输入按钮上,我有:
onclick=tex('#test').dit();
. 。 When I try to run the code I get an error that says it is undefined.
当我尝试运行代码时,出现错误消息,提示未定义。 Does anyone know what is wrong with my code?
有人知道我的代码有什么问题吗?
document.getElementById accepts only the id of the element itself without '#'. document.getElementById仅接受元素本身的ID,而不包含“#”。 The hash notation is used in jquery and css to indicate an id but to target the element itself you just use the id - in this case 'test'.
在jquery和css中使用哈希表示法来表示id,但仅使用id来定位元素本身即可,在这种情况下为“ test”。
To make your script run you need to remove the '.' 要运行脚本,您需要删除“。” or '#' before you call getElementById or getElementByClass respectively.
或“#”,然后分别调用getElementById或getElementByClass。
You could use the javascript substring method to achieve this. 您可以使用javascript 子字符串方法来实现此目的。
Edit: Also refer to Pointy's comment on the function .tex not having a return statement. 编辑:也请参考Pointy对函数.tex的注释,该注释没有return语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.