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!
    已采纳该答案
    打赏 评论
  • duanhuiqing9528 2018-08-07 18:12

    It looks like you're loading in the scripts outside of the body. Make sure to load them inside your <head> tags.

    打赏 评论
  • dre93205 2018-08-07 18:58

    here's a copy and paste from the revision history of the question, in order to demonstrate how the HTML markup should look alike, according to the op's description ...

    file index.php:

    <html>
        <head>
            <script src="../js/jquery-3.3.1.js"/>
            <script src="../js/jquery_color_animation.js"/>
            <script src="../js/script/homepage_script.js"/>
        </head>
        <body>
    
            <?php include 'header.php'; ?>
    
            <!-- further content goes here -->
    
        </body>
    </html>
    

    file header.php:

    <div class="userbox_rectangle_full_header">
        <div class="work_space" id="homepage_work_space">HOMEPAGE</div>
        <div class="work_space" id="userpage_work_space">USERPAGE</div>
    </div>
    

    file homepage_script.js:

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

    I'd suggest Twig or any other PHP template-engine, which are frameworks specialized on rendering HTML markup, including template bits, etc.

    打赏 评论

相关推荐 更多相似问题