【问题标题】:Pass header script to AJAX loaded content将标头脚本传递给 AJAX 加载的内容
【发布时间】:2015-01-06 16:36:32
【问题描述】:

当我搜索信息时,jquerymobile (JQM) 中的 ajax 加载不包含标头 javascripts。参考题目如下:

jQuery mobile tap event triggered for twice

http://demos.jquerymobile.com/1.1.1/docs/pages/page-scripting.html

我想知道 ajax 内容如何包含头脚本。

我的示例脚本(不是真正的脚本)是

在 index.php 中

<header>
<script type="text/javascript" src="script-test.js"></script>
</header>

<div id="id<?php echo $product['product_id'] ?>">
    Content<?php echo $product['product_id'] ?>
</div>

<script type="text/javascript"><!--
 ajaxload content scripts
//--></script>

在 index-ajax.php 中

<div id="id<?php echo $product['product_id'] ?>">
 Content<?php echo $product['product_id'] ?>
</div>

**我尝试手动添加到 index-ajax.html。但是,它会触发错误(加载2次js)到其他不在ajax内的内容。

【问题讨论】:

  • 您的php 开始标签错误。它应该是 &lt;?php 而不是 &lt;php
  • @Nikos 感谢和提​​炼!
  • 您无法命名文件(index.html、index-ajax.html)并在其中运行 PHP 代码。如果您将 HTML 与 PHP 混合使用,则需要将它们重命名为 (index.php, index-ajax.php)跨度>
  • @Tasos 谢谢,我已经完善了描述

标签: javascript php jquery ajax jquery-mobile


【解决方案1】:

根据jQuery Mobile FAQ: Why aren't my scripts and styles loading?

通过 AJAX 请求时忽略页面头部的原因是重新执行相同 JavaScript 的可能性非常高(在网站的每个页面中引用相同的脚本很常见)。由于尝试解决该问题的复杂性,我们将执行特定页面脚本的任务留给开发人员,并假设每个浏览会话只执行一次头部脚本。

您需要以某种方式将脚本绑定到 pageinit 事件,或者您可以将其包含在 data-role="page" 中,但这将在每次加载页面时执行。

我肯定会建议重构您的代码并根据pageinit 事件进行处理。

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多