![](/img/trans.png)
[英]How can I call click function to another click function in jQuery
[英]click function call with $(this) from another function in javascript/jquery
我有一個點擊功能,如下所示
$('.page-nav li').click(function(event){
console.log("clickedTab-page-nav-first-before set ="+Session.get('clickedTab'));
Session.set('clickedTab',event.target.id);
//var sel = event.prevUntil("[class*=active]").andSelf();
var sel = $(this).prevUntil("[class*=active]").andSelf(); //find all previous li
//of li which have
//class=active
sel = sel.add(sel.eq(0).prev()); // include the that li also(Now all li elements).
sel.removeClass('active'); //Remove the active.
//sel = event.nextUntil("[class*=active]").andSelf(); //Also for top to bottom
//(Viceversa)
sel = $(this).nextUntil("[class*=active]").andSelf();
sel = sel.add(sel.eq(-1).next());
sel.removeClass('active');
//event.addClass('active');
$(this).addClass('active'); //Now add class active for the clicked li
var rightcontent="";
console.log("clickedTab-page-nav-second-after set = "+Session.get('clickedTab'));
switch($(this).attr('id')){
case 'rfq':
.......
.....
}
});
接下來我想從另一個地方調用這個點擊功能
$(document).ready(function() {
console.log("clickedTab-page load = "+Session.get('clickedTab'));
if(Session.get('clickedTab')!=null||Session.get('clickedTab')!= undefined){
alert("Got It");
//$('.page-nav li').click(event);
$('.page-nav li').click(); //this is not working
}
});
現在問題是如果條件不起作用的頁面點擊功能。 但是我得到了警報。 任何幫助都非常感謝。 提前致謝...
你需要把$('.page-nav li').click(function(event){
里面document.ready
和你之前 $('.page-nav li').click();
因為如果你打電話.click
在DOM未准備好時.click
,可能沒有附加事件處理程序
如果你沒有放$('.page-nav li').click(function(event){
inside document.ready
OR你正在處理動態創建的元素。你需要委托事件$(document).on("click",".page-nav li",function(event){
你實際上並沒有在你的函數中使用event
參數,並且你聲明你希望在事件鏈之外調用它,所以你可以將它改為常規函數
var setupClicktab = function(id){
console.log("clickedTab-page-nav-first-before set ="+Session.get('clickedTab'));
Session.set('clickedTab',id);
...
}
你會像以下一樣使用它:
$('.page-nav li').click(function(event){return setupClicktab(event.target.id);});
並准備好文件
setupClicktab.Call($('.page-nav li'),Session.get('clickedTab'));
后者在選擇的上下文中調用它(即函數內部將引用選擇(1)。它還將存儲在會話變量中的值作為id傳遞。
旁注。 你的考試
if(Session.get('clickedTab')!=null||Session.get('clickedTab')!= undefined)
可能只是
if(Session.get('clickedTab'))
除非你可以在該變量中存儲空字符串,零或布爾值false。 但是看看它是如何使用的,因為它們都是id屬性的無效值
(1)這與它引用DOM元素的click事件略有不同)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.