jQuery ajax 调用错误地嵌套 php 生成的 html
我正在使用 jQuery 劫持指向 php 文件的链接。这个php文件主要是html,但是调用了两个php函数,每个函数都使用for循环来输出html。我遇到的问题是ajax调用错误地嵌套了php for循环生成的html。
将 php 文件直接包含到页面中效果很好,但是当尝试通过 ajax 获取它时,循环中每个顺序传递的 html 内容都会插入到先前生成的 html 中。例如:
<div>Content1</div>
<div>Content2</div>
<div>Content3</div>
变为:
<div>Content1<div>Content2<div>Content3</div></div></div>
这是 php 文件的相关代码:
<div class="publishedPosts">
<h3>Published</h3>
<?php displayPublishedBlogPosts(); ?>
</div>
<div class="verticalDivider"></div>
<div class="draftPosts">
<h3>Saved Drafts</h3>
<?php displayBlogPostDrafts(); ?>
</div>
这是 jQuery 代码:
function adminPanelTabs() {
$('#adminPanelTabs ul li a').click(function(e) {
$('#adminPanelTabs ul li a.current').removeClass('current');
$(this).addClass('current');
$('#adminPanelContent').load($(this).attr('href'));
e.preventDefault();
});
}
如果您有任何有关如何解决此问题的意见或建议,我们将不胜感激。
I'm using jQuery to hijack a link pointing to a php file. This php file is mostly html, but calls two php functions that each use a for loop to output html. The problem I'm running into is that the ajax call is incorrectly nesting the html generated by the php for loop.
Including the php file directly onto the page works fine, but when trying to grab it via ajax the html content of each sequential pass through the loop is inserted inside the previously generated html. For example:
<div>Content1</div>
<div>Content2</div>
<div>Content3</div>
becomes:
<div>Content1<div>Content2<div>Content3</div></div></div>
This is the relevant code for the php file:
<div class="publishedPosts">
<h3>Published</h3>
<?php displayPublishedBlogPosts(); ?>
</div>
<div class="verticalDivider"></div>
<div class="draftPosts">
<h3>Saved Drafts</h3>
<?php displayBlogPostDrafts(); ?>
</div>
And this is the jQuery code:
function adminPanelTabs() {
$('#adminPanelTabs ul li a').click(function(e) {
$('#adminPanelTabs ul li a.current').removeClass('current');
$(this).addClass('current');
$('#adminPanelContent').load($(this).attr('href'));
e.preventDefault();
});
}
Would appreciate any input or suggestions on how to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想想看——花几个小时研究如何解决这个问题,作为最后的手段在这里发布,然后我在 30 分钟后找出问题所在。
根据记录,问题出在被调用的 php 函数上。它们包括将结束 div 标记包含在 if 语句中的部分。
if 语句只是检查用户是否已登录,即使这是 true ,显然以这种方式进行 ajax 调用会失败这样的 if 语句,因此从未添加结束 div 标签。
我要感谢尼克的回复,因为这有助于引发我对问题可能出在哪里的想法。
Go figure - spend hours researching how to fix this, post here as a last resort, then I figure out what the problem is 30 minutes later.
For the record, the problem was in the php functions being called. They included sections where the closing div tag was enclosed in an if statement.
The if statement simply checked to see if the user was logged in, and even though this was true apparently making an ajax call in this way fails such an if statement, hence the closing div tag was never added.
I'd like to thank Nick for responding, as that helped trigger an idea about where the problem might be.