Javascript对象在HTML中工作,但不是PHP [英] Javascript objects working in html but not php

查看:112
本文介绍了Javascript对象在HTML中工作,但不是PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用文本框的网站,并根据用户提交的内容进行操作。

I'm working on a website that uses a textbox and does something with what the user submits.

在我的索引页面上,我不使用PHP(所有HTML ),我想使用JavaScript标签工作得很好。但是,在用户键入并提交之后,下一个加载的页面将包含PHP。 PHP页面调用一个函数来打印HTML,但即使这个页面与前一个索引页面完全相同,这些标签根本不起作用。

On my index page where I use no PHP (all HTML), Javascript tabs that I would like to use work perfectly fine. However, after the user types something in and submits, the next loaded page has PHP. The PHP page calls a function to print the HTML, but even if this page is the exact same as the previous index page, the tabs don't work at all.

...
<article id="navtabs" class="grid col-full">
    <h2>Tabs</h2>
    <div class="">
        <ul class="tabs clearfix">
            <li><a href="#tab1">First</a></li>
            <li><a href="#tab2">Second</a></li>
            <li><a href="#tab3">Third</a></li>
        </ul>
        <div class="tab_container">
            <article id="tab1" class="tab_content">
                <p>Lorem ipsum dolor sit amet</p>
            </article>

            <article id="tab2" class="tab_content">
                <h6>Heading</h6>
                <p>Lorem ipsum dolor sit amet</p>
            </article>

            <article id="tab3" class="tab_content">
                <h6>Heading</h6>
                <p>Lorem ipsum dolor sit amet</p>
            </article>
         </div>
      </div>
</article>
...

我的scripts.js文件中的Javascript代码如下所示:

The Javascript code in my scripts.js file looks like this:

$(document).ready(function() {
...

//TABS
var tabContents = $(".tab_content").hide(), 
    tabs = $("ul.tabs li");

tabs.first().addClass("active").show();
tabContents.first().show();

tabs.click(function() {
    var $this = $(this), 
        activeTab = $this.find('a').attr('href');

    if(!$this.hasClass('active')){
        $this.addClass('active').siblings().removeClass('active');
        tabContents.hide().filter(activeTab).fadeIn();
    }
    return false;
}); 
...
});

任何想法为什么只在使用HTML时才能使用,但在放入PHP函数时却没有?

Any ideas why this would work when using HTML only but not when put inside a PHP function?

编辑以澄清:在可用的版本中,它只是简单的HTML。在这个版本中,我没有做HTML的回声。这是失败的版本。

Edit for clarification: In the version that works it's simply plain HTML. In the version that doesn't I'm doing an echo of the HTML. This is the version that fails.

推荐答案

如果HTML是正确的并且javascript是正确的并且已加载那么它没有区别HTML是来自静态文件还是使用PHP生成的。浏览器仍将其视为HTML(PHP是服务器端,因此浏览器永远不会看到它),并且它在浏览器中运行javascript。

If the HTML is right and the javascript is right and is loaded then it makes no difference whether the HTML came from a static file or was generated with PHP. The browser still sees it as just HTML (the PHP is server-side so the browser never sees that) and it is in the browser that the javascript runs.

哪一个离开我们有以下几种可能:

Which leaves us with the following possibilities:


  1. 通过PHP生成的HTML在某种程度上是不同的(我知道你说它不是,但是双检查)

  2. 第二次没有加载javascript文件

这是假设你正在加载用PHP生成的整个页面 - 如果你开始添加yjax请求的位,那么当你在文档就绪中设置标签代码并且只看到页面时,情况会发生变化。

THis is assuming you are loading a whole page generated with PHP - if you start adding bits ysing ajax requests then the situation changes as you tab code is set up in document ready and only sees the page as it is then.

这篇关于Javascript对象在HTML中工作,但不是PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆