【发布时间】:2009-08-21 19:28:26
【问题描述】:
问题
我尝试使用以下
// Start method 1
var grbData = $.ajax({
type : "GET",
url : "http://grb.sonoma.edu:81/getgrbs.php",
data : "start=0&perPage=3"}).responseText;
$("#ajaxDiv").html(grbData);
// End method 1
// Start method 2
$.get("getgrbs.php", { start : 0, perPage : 3},
function(data) {
$("#tst").html(data);
}, "html");
// End method 2
在此页面上:http://grb.sonoma.edu:81/paging.php 从数据库加载数据。方法 1 似乎仅在 IE8 中有效,但仅在页面刷新后才有效。首次加载页面时,我收到“完成此操作所需的数据尚不可用”。错误。
我更喜欢方法 1 的原因是因为它让我可以访问表中的各个行。例如。每一行都有一个类,“burst”。我正在使用
$(".burst").click(function() {
$(".burst").css("background-color", "");
$(this).css("background-color", "yellow");
});
单击时更改所选行的颜色。这似乎只适用于方法 1,而不适用于方法 2。
以上所有代码都封装在 $(document).ready() 中。我试过了
$("#ajaxDiv").load("getgrbs.php", { start : 0, perPage : 3});
但我得到的结果类似于方法 2。
如何让点击功能与方法 2 一起使用或让方法 1 在所有浏览器上工作而无需刷新?感谢您为此提供的任何帮助。
我需要在 ajax 中执行此操作(尝试了没有 jquery 的 ajax,也没有运气),因为页面上的其他内容不会随着用户浏览数据而改变。
解决方案附录(答案中的更好解决方案)
成功使用“成功”后,我注意到单击行并更改背景颜色的功能消失了。所以我做了以下,这似乎工作。不确定它是否是最好的方法。
var grbData = $.ajax({
type : "GET",
url : "http://grb.sonoma.edu:81/getgrbs.php",
data : "start=0&perPage=3",
dataType : 'html',
success: function (data) {
$("#ajaxDiv").replaceWith(data);
startInteraction();
}
});
function startInteraction() {
$(".burst").click(function() {
$(".burst").css("background-color", "");
$(this).css("background-color", "yellow");
});
}
【问题讨论】: