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() {
        // ...
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多