为什么这个简单的 jQuery 脚本不起作用?
首先是脚本:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将代码包装起来
或将其放在
body
的末尾。否则,代码会在元素存在之前运行,因此不会添加处理程序。You need to wrap your code in
or put it at the end of your
body
. Otherwise, the code gets run before your element exists so no handler gets added.