将jQuery函数添加到特定元素 [英] Add jQuery function to specific elements

查看:110
本文介绍了将jQuery函数添加到特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以通过 $添加新的jQuery函数。fn.someFunction = function()

但是,我只想将功能添加到特定元素。我试过这种语法,它不起作用 $('。someElements')。fn.someFunction = function()

However, I want to add functions to specific elements only. I tried this syntax and it doesn't work $('.someElements').fn.someFunction = function()

我想这样做,以便我可以在代码 $('someElements')的某个地方调用函数。someFunction();

I want to do this so that I can call the function like this somewhere in the code $('someElements').someFunction();

推荐答案

使用 .bind() .trigger()

$('button').bind('someFunction',function() {
    alert('go away!')
});


$('button').click(function(){
    $(this).trigger('someFunction');
});




从jQuery 1.7开始,。on()方法是
将事件处理程序附加到文档的首选方法。

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

这篇关于将jQuery函数添加到特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆