如何将事件和效果应用于通过 Ajax 请求生成的没有特定 id 的元素?
我正在制作一个论坛,我希望它像桌面应用程序一样运行,所以我不刷新页面。由于缺乏更好的方法而没有另一个完整的 Ajax 请求,我收到了 Ajax 请求中可用数据的页数。我需要显示这些数据(正如我在 ethoma.com/testhome.php 上所做的那样——我将页面大小设置为 1 进行测试),但我还需要向显示的每个单独数字添加事件处理程序,以触发将发生变化的事件文本的颜色并触发 Ajax 调用以获取指定的页码。对我来说,挑战是可能有 500 页(当然,这样我就无法显示每个页码)。对于那些不想通过我的网站查看代码的人,这里是它的重要部分:
function getPosts()
{
var queryString = "category=" + category + "&page=" + page + "&order=" + order;
$.ajax(
{
url: 'getposts.php',
data: queryString,
success: function(data)
{
var oldHtmlTemp;
var dataArray = data.split("%^&$;");
numpage = dataArray[0];
$('#postcontainer').html(dataArray[1]);
$('#enterpage').html('');
if (numpage != 0)
{
for(var i=1;i<=numpage;i++)
{
oldHtmlTemp = $('#enterpage').html();
$('#enterpage').html(oldHtmlTemp + " " + i);
}
oldHtmlTemp = $('#enterpage').html();
$('#enterpage').html(oldHtmlTemp + " ");
}
else
{
$('#enterpage').html('No results for this query');
}
}
});
}
如果您想知道 .split() 正在做什么,则 php 文档返回由那个奇怪的字符串分隔的页面数我指定的。我认为这是将页数放入帖子文本其余部分的最简单方法。
无论如何,我如何将事件处理程序添加到这些单独的数字中?
我有一个快速的后续问题,该代码由于某些奇怪的原因而无法工作。它将事件处理程序添加到下一页和上一页按钮,同时还会进行错误检查以确保您不会尝试点击页面 -1。
$("#nextpage").click(function()
{
if (page < numpage -1)
{
page++;
getPosts();
alert(page);
}
});
$("#prevpage").click(function()
{
if (page >= 1);
{
page--;
getPosts();
alert(page);
}
});
警报用于调试。另外值得注意的是,当 page = 0 时,您将获得第 1 页。我的意思是,我从 0 1 2 开始计数,但用户看到的是 1 2 3。
感谢任何查看/回答此帖子的人。
I am making a forum and I want it to run like a desktop application, so I do not refresh the page. For lack of a better method without another complete Ajax request I receive the number of pages of data available in an Ajax request. I need to display this data (as I have at ethoma.com/testhome.php -- I set the page size to 1 for testing) but I also need to add event handlers to each individual number displayed to trigger an event that will change the color of the text and trigger an Ajax call to get the page number specified. The challenge for me is that there could be 500 pages (of course then I wouldn't be able to display every page number). For those who don't want to view the code via my site, here is the important parts of it:
function getPosts()
{
var queryString = "category=" + category + "&page=" + page + "&order=" + order;
$.ajax(
{
url: 'getposts.php',
data: queryString,
success: function(data)
{
var oldHtmlTemp;
var dataArray = data.split("%^&$;");
numpage = dataArray[0];
$('#postcontainer').html(dataArray[1]);
$('#enterpage').html('');
if (numpage != 0)
{
for(var i=1;i<=numpage;i++)
{
oldHtmlTemp = $('#enterpage').html();
$('#enterpage').html(oldHtmlTemp + " " + i);
}
oldHtmlTemp = $('#enterpage').html();
$('#enterpage').html(oldHtmlTemp + " ");
}
else
{
$('#enterpage').html('No results for this query');
}
}
});
}
If you are wondering what the .split() is doing, the php doc returns the number of pages seperated by that weird string that I designated. I decided it would be the easiest way to put the number of pages within the rest of the post text.
Anyway how would I add event handlers to these individual numbers?
I have a quick follow-up question, this code isn't working for some weird reason. It adds an event handler to the next page and previous page buttons, but also error checks to make sure you aren't trying to hit page -1.
$("#nextpage").click(function()
{
if (page < numpage -1)
{
page++;
getPosts();
alert(page);
}
});
$("#prevpage").click(function()
{
if (page >= 1);
{
page--;
getPosts();
alert(page);
}
});
Alerts are for debugging. Also worth noting is that when page = 0, you get page 1. What I mean is, I am counting from 0 1 2, but the user sees 1 2 3.
Thanks to anyone who views/answers this post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我先参考最后一个问题。
我不明白后续问题,因为您没有具体说明什么不起作用。
我猜你在动态加载新 HTML 时会覆盖“下一个”、“上一个”。
要解决此问题,请查看 “live”jquery 方法。
它的作用与分配“点击”完全相同(就像您所做的那样),但它会重新评估每个事件的选择器。所以新的元素仍然会被包含在内。
我相信“实时”方法也适用于第一个问题。
只需用由唯一“类”标识的一些“范围”来包装每个数字。并添加如下内容:
当我编写动态加载的 HTML 时,我总是将 Javascript 分配给该 HTML。
这样,当新的 HTML 加载时,我会重新评估 JS。
例如 - 我返回的 html 看起来像这样
它给我的好处是我将行为分配给数据。 (就像 OOP 中一样)。
我不需要在每次加载 HTML 时重写行为。 (在此示例中突出显示文本)。
每次加载 HTML 时它都会突出显示,因为脚本会被评估。
您应该考虑使用此设计模式,因为它将:
I will refer to the last question first.
I didn't understand the followup question as you didn't specify what exactly is not working.
I am guessing you are overriding your "next","prev" while dynamically loading new HTML.
To resolve this, take a look in the "live" jquery method.
What it does is exactly like assigning "click" (like you did) but it re-evaluates the selector on each event. So the new elements will still be included.
I believe the "live" method will work on the first question as well.
Simply wrap each number with some "span" identified by a unique "class". and add something like this :
When I write an HTML that loads dynamically, I always assign the Javascript to that HTML.
This way - I reevaluate the JS when the new HTML is loaded.
For example - my returned html looks something like
The benefit it gives me is that I assign the behavior to the data. (just like in OOP).
I don't need to rewrite the behavior for each time I load the HTML. (in this example to highlight the text).
It will be highlighted each time I load the HTML because the script is evaluated.
You should consider using this design pattern as it will :