dongwen9975 2018-08-07 18:08
浏览 81
已采纳

Jquery选择包含在php文件中的div元素?

I'm pretty new to JQuery and I may be missing a few things about it.

I have created a .php file which contains the header of my website. I included it into my main .php file. The "header.php" contains a 'div class= userbox_rectangle_full_header' that I would like to select from a JQuery loaded in the main "homepage.php".

I tried selecting it to show an alert on the click of the 'div'. When the Jquery is loaded in the "header.php", no problem, the alert correctly shows. But nothing happens when the .js file is loaded in "homepage.php".

Hope you'll be able to help me about what I'm missing.

The main homepage in which I load the JQuery:

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8" />
        <title>DSI Welcome</title>
        <link rel="stylesheet" href="../css/homepage.css" />
    </head>
    <body>
        <header>
            <?php include 'header.php';?>
        </header>

        <div class="work_space" id="homepage_work_space">HOMEPAGE</div>
        <div class="work_space" id="userpage_work_space">USERPAGE</div>
    </body>
    <script src="../js/jquery-3.3.1.js"></script>
    <script src="../js/jquery_color_animation.js"></script>
    <script src="../js/script/homepage_script.js"></script>
</html>

The "header.php" file which I include into the main "homepage.php":

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Header</title>
        <link rel="stylesheet" href="../css/header.css" />
    </head>
    <body>
        <header>
            <div class="userbox_rectangle_full_header">&nbsp</div>
        </header>
    </body>
    <script src="../js/jquery-3.3.1.js"></script>
    <script src="../js/jquery_color_animation.js"></script>
    <script src="../js/script/header_animation.js"></script>
</html>

Finally, the JQuery code which is not working when loaded in the "homepage.php", but working when loaded in the "header.php":

$(document).ready(function() {
    $('#homepage_work_space').on('click', function(){
        alert('hi');
    });
});

Thank you in advance !

  • 写回答

3条回答 默认 最新

  • dourao1896 2018-08-07 18:46
    关注

    There is some points in your code thats better to know .

    1. Your html structure is wrong! simple html5 structure:

      <!DOCTYPE html> <html> <head> <title>Title of the document</title> <!-- Your JQuery Main File --> </head> <body> Content of the document...... </body> </html>

    2. Define your main jquery file in head of your body.
    3. Your document has 2 <body> tag . (one your main body and other included from header.php )
    4. Define your script files(except jquery that defined in head) in the footer , just before </body>
    5. And in your header.php file dont define any script!
      Have a good time!
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题