为什么这个简单的 jQuery 脚本不起作用?

发布于 2024-12-15 03:13:50 字数 801 浏览 5 评论 0原文

首先是脚本:

<script type="text/javascript" language="javascript">
    <!--
    <?php
    echo("var answersC = 'answertable'+$qrow->id;");
    echo("var toggleC = 'toggletable'+$qrow->id;");
    ?>
    jQuery('#'+toggleC).click(function() {
    jQuery('#'+answersC).slideToggle('fast');
    });
    console.log('#'+toggleC); //example #toggletable6
    console.log('#'+answersC); //example #answertable6
    // -->

我将其发布在另一个线程中,以了解如何使用 id 名称的变量,但现在可以正常工作,没有错误......但也没有结果

详细信息

  • 此脚本位于 PHP 页面中,该页面为某个项目呈现一个管理页面。 WordPress 插件。
  • 它位于结果循环内,目的是 逐个问题地切换答案列表的显示 基于唯一ID的基础。
  • 我使用几乎相同的脚本 将成功显示所有问题的所有答案 他们的共同班级。
  • 我已经验证了 div id 正在显示 正确地在 html 和变量中。

First the script:

<script type="text/javascript" language="javascript">
    <!--
    <?php
    echo("var answersC = 'answertable'+$qrow->id;");
    echo("var toggleC = 'toggletable'+$qrow->id;");
    ?>
    jQuery('#'+toggleC).click(function() {
    jQuery('#'+answersC).slideToggle('fast');
    });
    console.log('#'+toggleC); //example #toggletable6
    console.log('#'+answersC); //example #answertable6
    // -->

I posted this in another thread to workout how to use the variables for the id name, but now that is working properly with no errors... but also no result.

Details:

  • This script sits within the PHP page that renders an admin page for a
    WordPress plugin.
  • It is within a loop of results and the intent is to
    toggle the display of a list of answers on a question by question
    basis based on a unique id.
  • I use a nearly identical bit of script
    that successfully will display all answers to all questions based on
    their common class.
  • I have verified that the div ids are showing
    properly in the html and the variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-12-22 03:13:50

您需要将代码包装起来

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

或将其放在 body 的末尾。否则,代码会在元素存在之前运行,因此不会添加处理程序。

You need to wrap your code in

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

or put it at the end of your body. Otherwise, the code gets run before your element exists so no handler gets added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文