最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass a value to trigger click function - Stack Overflow

programmeradmin0浏览0评论

I've got 3 buttons and I've used their calss to create a click function.

$('.abc').click(function(){
     if (val).is(':visible')){
       //do something
     }
});

I've got another click function as below

$(".custom-rmenu li").click(function(){
     // This is the triggered action name
     switch($(this).attr("data-action")) {
     // A case for each action.
       case "case1": alert("first"); break;
       case "case2": alert("second"); break;
       case "case3": alert("third"); break;
     }
});

My question is, I need to trigger the $('.abc'). click function via the $(".custom-rmenu li"). click function. How do I pass a value (val) from each case to the $('.abc'). click function?

I've got 3 buttons and I've used their calss to create a click function.

$('.abc').click(function(){
     if (val).is(':visible')){
       //do something
     }
});

I've got another click function as below

$(".custom-rmenu li").click(function(){
     // This is the triggered action name
     switch($(this).attr("data-action")) {
     // A case for each action.
       case "case1": alert("first"); break;
       case "case2": alert("second"); break;
       case "case3": alert("third"); break;
     }
});

My question is, I need to trigger the $('.abc'). click function via the $(".custom-rmenu li"). click function. How do I pass a value (val) from each case to the $('.abc'). click function?

Share Improve this question asked May 25, 2015 at 5:18 BeckyBecky 5,5959 gold badges43 silver badges78 bronze badges 2
  • Here is demo please check it out. jsfiddle/0yu2zx3u – Dhaval Bharadva Commented May 25, 2015 at 5:42
  • How is your HTML built up? Is there a .abc button inside each <li>? – Ja͢ck Commented May 25, 2015 at 5:46
Add a ment  | 

3 Answers 3

Reset to default 6

You can pass parameters in trigger as follow:

$( ".abc").trigger( "click", [ "myParam1", "myParam2" ] );

And you can get the parameters:

$('.abc').click(function(e, myParam1, myParam2) {

Docs: http://api.jquery./trigger/

trigger signature: .trigger( eventType [, extraParameters ] )

In jQuery trigger function first parameter is the event to be triggered, you can pass any other data as second argument in array, like this:

.trigger('click', [val1, val2]);

.on('click', function(event, val1, val2) { ... })

An incredible use of this could be like an "observer" from a class that you can't access because the click is been recognized by a vendor that produce a JS animation.

So, after executing the the animation in vendor you could call an listener inside in your main side effect class, triggering an observer.

First create and a DOM object to be a observer:

<a id='#observer'></a>

In your main class, declare the observer:

this.observer = $('#observer');

In the same class, define your function to handle listeners:

registerHandlers() {
    this.observer.off().click((e, params) => this[params.function](params.event));
}

In last, trigger the click inside the vendor, after animation:

$('vendor-class').on('click', function (event) {
    animationJs() // Vendor stuff...
    $('#observer').trigger( "click", [ {
      event,
      function:"myFunctionToHandleVendor",
    } ] );
}

NOTE:. The great achievement here is pass an object containing multiples variables, for this put the object inside an array.

[{type:'secret', user:'Jonh', msg:'Paul is dead!'}]
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>