dongmale0656 2015-11-06 17:02 采纳率: 100%
浏览 49
已采纳

jquery .load()和访问主机页面的脚本

I've read through a few posts here regarding this topic, but none seem to answer which method is the best for allowing sub-pages access to the host page's <script> section.

I load home.php, which has a menu displayed in one of its two <div> sections. The HTML for this menu is on home.php itself. The user is able to interact with buttons and dropdowns in the menu portion of home.php through scripts on home.php, like:

$("#graphsButton").click(function() {  
  if($(this).text()=="Graphs") {
    $(this).html("<span><i class='fa fa-check-circle checked'></i> Graphs</span>");
    graph = true;
  } else {
    $(this).html("<span>Graphs</span>");
    graph = false;
  }   
});

After the user performs their initial operations, that larger menu is replaced by a smaller menu to provide the user more screen real-estate, using the following code:

function shrinkMenu() {

  $('#search').html('');
  $('#search').animate({
    width: '4%'
  }, 500);
  $('#returnMain').animate({
    width: '96%'
  }, 500);
  $('#search').load('smallMenu.php');
}

If the user wants the big menu back, on smallMenu.php I then have another <script> section with this code:

$('#growMenu').click(function () {
  $('#search').html('');
  $('#search').animate({
    width: '20%'
  }, 500);
  $('#returnMain').animate({
    width: '80%'
  }, 500);
  $('#search').load('largeMenu.php');    
});

largeMenu.php contains a duplicate copy of the original HTML for the menu that loaded along with home.php, and visually, it looks exactly the same to the user.

As I toggle between large menu and small menu however, the <script> sections contained in home.php that pertain to the id tags in the original menu HTML that loaded with home.php no longer work.

In order to make it work, it seems that I would have to re-initialize all my plugins on each page load, and maintain 3 separate pages' <script> sections that are all duplicates of one another, and this seems very inefficient and probably not the best way to go about this.

Is there a better way for me to achieve the functionality I am trying to code here?

Update

My code looks like this at a high level:

<div class="searchParent" id="search">
  <div class="return"></div>
  <div class="menu">
    <div class="largeMenu" id="largeMenu"></div>
  </div>
</div>
  • 写回答

1条回答 默认 最新

  • douji1999 2015-11-06 17:18
    关注

    What you are doing is fine by replacing the menu from a php file that contains a "partial view" which is your menu HTML. The partial view relies upon the parent page code, so you don't need to duplicate any code from the parent script into the partial view. The partial view can expect there is script code on the parent page it can be used with.

    As I toggle between large menu and small menu however, the sections contained in home.php that pertain to the id tags in the original menu HTML that loaded with home.php no longer work.

    The problem you are having though is that you need to ensure that the code on home.php that references these menus uses a parent container as its reference point instead of direct IDs. You are replacing elements (menus), so the bindings are lost when you do that unless you bind on a parent higher up the DOM that doesn't get replaced (remember that events bubble up).

    From what I see in your code snippets, it looks like the menu is loaded into the #search container. So just ensure the code in home.php uses that container as the reference point, using something like on like this:

    $('#search').on('click', '#graphsButton', function() {
        // ...
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法