简体   繁体   English

使用 jQuery 选择 php 包含的 html 元素

[英]Select php included html element with jQuery

I'd like to select an included html element with jQuery.我想用 jQuery 选择一个包含的 html 元素。 How can I achive this?我怎样才能做到这一点?

index.php索引.php

<?php
    <html>
        <head>
            ...
        </head>
        <body>
            include('file.php');
        </body>
    <html>
?>

file.php文件.php

<?php
    echo '<div class="test">test</div>';
?>

select.js选择.js

let a = $('.test');
console.log(a); // returns undefined

Try executing your script after the page has fully loaded.在页面完全加载后尝试执行您的脚本。

$(window).on("load", function() {
   let test = $('.test');
   console.log(test);
}); 

Code should be organized as follows: <?php Php.Code.Goes.Here ?> .代码应组织如下: <?php Php.Code.Goes.Here ?> This can be surrounded by HTML, for instance, <b><?php print("HELLO!"); ?></b>这可以用 HTML 包围,例如<b><?php print("HELLO!"); ?></b> <b><?php print("HELLO!"); ?></b> . <b><?php print("HELLO!"); ?></b> . This will display: HELLO!这将显示:你好!

So...you see the problem then, with...所以......你看到了问题,然后......

<?php
    <html>
        <head>
            ...
        </head>
        <body>
            include('file.php');
        </body>
    <html>
?>

The HTML is IN the PHP.该HTMLPHP。 You want this:你要这个:

<html>
    <head>
        ...
    </head>
    <body>
        <?php include('file.php'); ?>
    </body>
<html>

This code, of course, shouldn't have executed at all, since <?php <html> isn't really valid PHP syntax.当然,这段代码根本不应该执行,因为<?php <html>并不是真正有效的 PHP 语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM