【问题标题】:call a function inside a jQuery plugin from outside on-click event从外部点击事件调用 jQuery 插件内部的函数
【发布时间】:2015-04-01 12:43:03
【问题描述】:
我正在使用以下库来自动完成并在我的应用程序中提及用户名。我的问题是,我怎样才能触发 init: 来自锚标记的 onClick 的函数,而不是在文本区域中写入。有什么建议吗?这是我尝试编写的一个函数。
这是插件
http://podio.github.io/jquery-mentions-input/
$("a").on('click', '.tag_user_btn', function(event) {
event.preventDefault();
var txtPost = $(this).parents('.post-comment').find('textarea');
txtPost.val(txtPost.val()+" @a").focus().init();//call initilize function of library
});
【问题讨论】:
标签:
javascript
jquery
mention
【解决方案1】:
如果您查看文档(“方法”部分),init() 方法实际上是在插件实例上公开的。示例:
var mention = $('textarea.mention').mentionsInput({}); // Apply the plugin
mention.init(); // call the init() method
【解决方案2】:
不确定这是问题的根源,但
$("a").on('click', '.tag_user_btn', function(event) { //WRONG
这是错误的。 jQuery documentation for .on()
正确的语法$(parentSelector).on('click', 'childSelector', function()
改为使用(用于 ajax 事件处理)
$(document).on('click', 'a.tag_user_btn', function(event) {
或用于非 ajax 事件处理
$('a.tag_user_btn').click(function(event) 是
$('a.tag_user_btn').on('click', function(event))