【发布时间】:2021-03-02 22:56:02
【问题描述】:
我想用 jQuery 选择一个包含的 html 元素。我怎样才能做到这一点?
index.php
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
文件.php
<?php
echo '<div class="test">test</div>';
?>
select.js
let a = $('.test');
console.log(a); // returns undefined
【问题讨论】:
-
确保 js 代码在 html 元素出现在 DOM 或
document.ready事件后运行。 -
在DOM结构中test是否显示?
-
@sergeykuznetsov 是的,它在 DOM 结构中。
-
@u_mulder 我已经尝试过使用 document.ready 但它不起作用:/
-
<?php <html> ...Umm.... 尝试将<?php放在 PHP 调用之前,并在再次启动 HTML 之前结束它。即<?php include('file.php'); ?>。代码甚至不应该像现在这样执行。