如何在jquery中创建全局函数,并从另一个加载的页面调用它

问题描述:

如何在jquery中声明全局函数,如何从使用jquery的load()函数加载到该页面上某个div的页面中调用它.

how to declare a global function in jquery, how do i call it from a page that was loaded in some div on that page using jquery's load() function.

该功能在第一个子页面


+-----------------------------------------------+
| main links                                    |
+-----------------------------------------------|
| +-------------------------------------------+ |
| |1st sub page (myfun function is here)      | |
| +-------------------------------------------+ |
| | +---------------------------------------+ | |
| | |                                       | | |
| | |                                       | | |
| | |                                       | | |
| | | mybutton clicked myfun called         | | |
| | |                                       | | |
| | +---------------------------------------+ | |
| +-------------------------------------------+ |
+-----------------------------------------------+

但是当我单击时,什么也没有发生... 这都是函数

But when i click, nothing happens... here are both function

myfun

myfun(tab_index, acc_id){
    alert(tab_index +" | +" acc_id);
}

我的按钮

$("#mybutton").click(function(){
    var $bottomLineBtn = $(this);
    $bottomLineBtn.parent().myfun('2','43234');
})

有人可以帮我吗..太难找到解决方案了..我已经搜索了3个小时...

Can somebody please help me.. its too hard to find the solution.. i've been searching for 3 hours...

============================================= ======================================

已更新

这是详细情况

  1. 我正在使用jquery处理一些动态的东西
  2. 第一页包含2个div,一个为id=menu,另一个为id=subpage1
  3. 我单击#menu中的一个链接,将页面加载到#subpage1.
  4. 再次有一个页面包含2个divs #menu2#subpage2
  5. 在这里,我制作了一个脚本,该脚本通过仅使用一个参数....这是列表菜单的ID来自动将页面加载到#subpage2.
  6. 此脚本是..

  1. i'm working on some dynamic stuff using jquery
  2. the first page contains 2 divs, ones id=menu and others id=subpage1,
  3. i click on one link from #menu, a page is loaded into #subpage1.
  4. there is again a page with 2 divs #menu2 and #subpage2
  5. here i made a script that loads pages automatically to #subpage2 by taking only 1 parameter ....which is id of a listmenu.
  6. this script is..


 $("#menu2 li").click(function(){
     $("#subpage2").load($(this).attr('page_link'));
   }
 }).filter('#menu2 li:eq(0)').click();
 

  • 在加载的页面中.在最底端,我使用2个按钮,一个按钮可以正常工作,它调用上面的函数并更改我这样做的值li:eq(1)..

  • in the loaded page. at extreme bottom, i use 2 buttons, one button works fine, it calls the above function and change the value li:eq(1) .. that i did like this.

    
        $("#bottom_bar #backbtn").click(function(){
        var $bottomLineBtn = $(this);
        $bottomLineBtn.parent().parent().parent().find('#menu2 li:eq(1)').click();
        })
    

    ...但是另一个按钮不起作用.我想调用#subpage1中的一些div所属的函数.但无法访问它们.在上述$("#menu2 li").click(function(){函数旁边,我只放了一个函数.

    ... but the other button doesn't work. i want to call a function that belongs with some divs in #subpage1. but can't access them. i put only a function, next to the above $("#menu2 li").click(function(){ function.

     myfun(tab_index, acc_id){
            alert(tab_index +" | +" acc_id);
        }
    

    但是我不知道如何从加载到#subpage2

    but i don't know how to call this function from the 2nd button loaded into #subpage2

    以上场景是否构成了场景..请尝试了解我..我的描述不是很好...

    does the above scenario make scene.. please try to understand me.. i'm not very good in description...

  • 为什么不能将函数放在外部.js文件中,然后将其导入到主页中?

    Why can't you put your function in an external .js file and then import it into your main page?