【问题标题】:How to make jQuery Mobile only affect one page [duplicate]如何使jQuery Mobile只影响一页[重复]
【发布时间】:2014-05-16 01:11:21
【问题描述】:

在我正在创建的网站中,我使用 jQuery Mobile,但我对此并不了解,而且我遇到了一个大问题。在我的网站中,我只在一个页面上链接到 jQuery Mobile,我只想在一个页面上使用 jQuery Mobile(我不知道这是否可能,但这就是我想要做的)。

我的网站上有四个主要页面:Homepage.php、SalesForms.php、Reports.php 和 Login.php。我只想在 Reports.php 页面上使用 jQuery Mobile(我使用它来进行转换以创建一个漂亮的 html 表单流程)。

每当我访问 Reports.php 页面时,它都会使用 jQuery Mobile 和 jQuery 以及 jQuery Mobile CSS,这很好。但是当我离开 Reports.php 页面并转到其他页面时,jQuery Mobile 及其 CSS 仍在使用中,即使它没有链接到那些页面上并且我不想使用它。

例如,当我访问 Homepage.php 时,它没有使用 jQuery Mobile 或其 CSS,然后我访问 Reports.php,它确实使用了 jQuery Mobile 及其 CSS,然后我再次访问 Homepage.php,它仍然是使用 jQuery Mobile 及其 CSS。这对我来说是个问题,因为 jQuery Mobile 的 CSS 在 Reports.php 页面以外的页面上干扰了我的 CSS,Reports.php 页面是它唯一链接到的页面。

我想知道是否以及如何使 jQuery Mobile 仅在 Reports.php 页面上使用,这样如果我在访问 Reports.php 页面后转到另一个页面,它就不会使用 jQuery Mobile。

这是我链接到 jQuery Mobile 的代码(我只在 Reports.php 页面中链接到 jQuery Mobile):

<head>
    <link rel="stylesheet" href="../css/jQuery.css">
    <link rel="stylesheet" href="../css/Main.css">
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>

jQuery.css 只是 jQuery 的 CSS 的修改版本,因此它不会干扰我的 CSS,而 Main.css 是我的 CSS。

【问题讨论】:

    标签: jquery jquery-mobile webpage


    【解决方案1】:

    我假设您没有使用 mod_rewrite,并且您的文件 reports.php 在 url 上可见。

    根据您的评论:这是 $_SERVER["REQUEST_URI"] 的输出:

    string(47) "/~a7068104/2013-2014/Lab_13/Reports/Reports.php"
    

    所以,我们将使用 stripos() 来检查用户是否正在请求一个页面,其中包含“reports.php”!我们使用不区分大小写的函数而不是 strpos(),您可以在 (http://www.php.net/manual/en/function.stripos.php) 找到更多信息

    您可以使用以下条件/逻辑:

    <?php if ( stripos($_SERVER['REQUEST_URI'], "reports.php") ) : ?>
    
        <!-- add the html specific to the REPORTS.PHP -->
    
    
    <?php else: ?>
    
        <!-- add the html specific to NON REPORTS.PHP Pages -->
    
    <?php endif; ?>
    
    <!-- Remember, everything else GOES OUTSIDE THE PHP IF CLAUSE --->
    

    您的问题的解决方案是:

        <link rel="stylesheet" href="../css/jQuery.css">
        <link rel="stylesheet" href="../css/Main.css">
        <?php if ( stripos($_SERVER['REQUEST_URI'], "reports.php") ) : ?>
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
        <?php else: ?>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <?php endif; ?>
    

    希望这会有所帮助!

    【讨论】:

    • 我没有使用 mod_rewrite 但这不起作用。我应该把它放在我的 CSS 和 jQuery 链接的地方,对吗?
    • 是的,如果 "reports.php" 在 REQUEST_URI 上,它应该只显示包装在 php if 子句中的 html。该页面的网址是什么?
    • 好的,刚改成 stripos() 它不区分大小写。注意到您可能会将您的请求大写。这肯定行得通!
    • 是的,试试最后的修改。它应该工作。告诉我!
    • 那也没用。感谢您的尝试!
    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2013-02-02
    • 1970-01-01
    • 2019-05-26
    相关资源
    最近更新 更多