类似 jQuery 的自定义函数
我一直想知道如何创建像 jQuery 这样的函数。例如: $(ID).function()
其中 ID 是 HTML 元素的 id,$
是返回元素的 document.getElementById 引用的函数 " ID”和函数是自定义 JavaScript 函数。
我正在创建一个实现一些功能的小库。我想在不使用 jQuery 的情况下使用该语法。
现在,我的问题是:我该如何实施?允许这样做的技术的名称是什么?
编辑:
我想做的是这样的:
HTMLElement.prototype.alertMe = function() {alert(this.value);}
然后,当我调用 document.getElementById('html_input_id').alertMe()
时,它必须显示一个带有以下内容的警报框:输入值。但是 HTMLElement.prototype
在 IE 中不起作用。
I have been wondering how I can create functions like jQuery. For example: $(ID).function()
Where ID is the id of an HTML element, $
is a function that return the document.getElementById reference of the element "ID" and function is a custom javascript function.
I'm creating a little library which implements some functions. And I want to use that sintax without using jQuery.
Now, my questions are: how I can implement that? What is the name of the tecnique that allow that?
Edit:
What I want to do is this:
HTMLElement.prototype.alertMe = function() {alert(this.value);}
Then, when I call document.getElementById('html_input_id').alertMe()
, it must show an alertbox with the input value. But HTMLElement.prototype
doesn't work in IE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你看,你问的问题有很多细节和含义。 jQuery的代码是开源的,您可以阅读它来了解详细信息;你也最好找到一本很好的 Javascript 书,《O'Reilly Definitive Guide》。
$
只是 JS 中名称的一个字符,因此正如其他一些答案所示,您没有理由不能只编写具有该名称的函数:因为每个人和他的兄弟都使用该技巧,你也想要一个更长的名字,这样你就可以混合使用。
由于您最终会使用入口点函数执行不同的操作,因此您需要了解 JS 如何使用参数 - 查找
arguments
对象。您需要将其打包,以便您的内部结构不会污染名称空间;您需要模块模式的一些变体,这将使它类似于
你最终会想要为继承做好准备,这意味着将这些函数放入
prototype
对象中。Okay, look, what you're asking has a lot of details and implications. The code for jQuery is open source, you can read it for the details; you'd do well to find a good Javascript book as well, the the O'Reilly Definitive Guide.
$
is just a character for names in JS, so as some of the other answers have shown, there's no reason you can't just write a function with that name:Since everyone and his brother uses that trick, you want to have a longer name as well, so you can mix things.
Since you end up doing different things with the entry point function, you need to know how JS uses arguments -- look up the
arguments
object.You'll want to package this so that your internals don't pollute the namespace; you'll want some variant of the module pattern, which will make it something like
And you'll eventually want to be prepared for inheritance and that means putting those functions into the
prototype
object.您可以使用prototype将新函数分配给Element原型。
这将为所有 HTML 元素提供函数“testFunction”。
您可以通过这种方式扩展任何基本对象,即数组、字符串等。
这将在没有任何插件的情况下工作 - 尽管我不认为它在 IE 中工作。我相信 MooTools 和 jQquery 等库会使用 DOM 元素创建自己的继承,以确保跨浏览器兼容性,尽管不要引用我的观点。
You can use prototype to assign a new function to the Element prototype.
This would provide the function 'testFunction' to all HTML elements.
You can extend any base Object this way, i.e. Array, String etc.
This will work without any plugin at all - although that said I don't think it will work in IE. I believe libraries such as MooTools and jQquery create their own inheritance with DOM elements to ensure cross-browser compatibility, although don't quote me on that.