这段代码有两个问题。
1$(container + ' meta[data-level="' + level + '"]').length == 0
始终为零我知道这一点,因为我创建了create_views(1);
不应添加的额外调用,但无论如何它都会被附加。
2 dpUniSlider 不起作用,因为它看不到通过 ajax 调用创建的 li。如果我将它移到成功消息中,它可以正常工作,但外部功能不能。问题是如果我将它包含在 ajax 成功中,它将被调用多次,因为它在循环下
//Show levels using ajax, before slider is activated
function create_views(level) {
$.ajax({
url: "actions.php",
type: "GET",
data: "show_level=" + level,
cache: false,
success: function (data) {
var view = '<li data-level="' + level + '">' + data + '</li>';
var container = ".slides_container";
if ($(container + ' meta[data-level="' + level + '"]').length == 0) {
$(container).append(view);
} else { //check if element exists, if yes overwrite it.
//$(container + ' meta[data-level="' + level + '"]').replaceWith(view);
alert("Exists");
}
}
});
}
//Loop through all levels and display views
//level count can be rewritten to come from DB and not be hardcoded like now
var levels = 2;
for (var i = 1; i <= levels; i++) {
create_views(i);
} // for loop
create_views(1); //test, delete this
//Activate slide
var unislider = $(".slides_container").dpUniSlider({
//loop: false,
draggable: false
});