使用ajax,jquery动态附加时,owl轮播项目无法正常工作

时间:2016-04-30 13:20:45

标签: jquery html owl-carousel

使用ajax,jquery动态追加时,

owl carousel项目无效 下面是我的ajax代码..任何帮助将非常感谢..

提前致谢

 $.ajax({
                type: "POST",
                url: "urllinks",
                data: 'id='+id,
                dataType: 'json',
                cache: false,
                success: function(data) {
                var locationoffers='';
                locationoffers+='<div id="owl-demo" class="owl-carousel">';
                $.each(data, function(i, item) {
                    locationoffers+='<div class="item"><a href="#" class="thumbnail"><img src="<?php echo base_url(); ?>assets/uploads/location_offers/'+data[i].image+'"  alt="Location Logo" /></a><span class="tocenter"> <h3>'+data[i].tittle+'</h3></span></div><div class="desc"><span> <p>'+data[i].message+'</p> </span></div><br><div class="tocenter"><a href="http://www.google.com" target="_blank" class="btn green">Grabit</a></div> </div>';
                });

            locationoffers+='</div>';

               $('#display_adds').html(locationoffers);
              $('.owl-carousel').owlCarousel();


        }


   });

     }   

This is the error output iam getting

2 个答案:

答案 0 :(得分:1)

当您在ajax调用后显示owl carousel时,您需要在收到响应后初始化它,因此请尝试将轮播放入函数中

  function init_carousel() {
     $('.owl-carousel').owlCarousel();
  }; 

并在ajax响应成功刷新轮播时调用该函数。并且还要在页面加载时初始化jus调用函数:

jQuery(document).ready(function() {
   init_carousel();
});

答案 1 :(得分:0)

除了上述答案外,请尝试使用owlCarousel属性将行为控制为:

import SwiftUI
import CoreData

@objc(ToDoItem)
public class ToDoItem: NSManagedObject , Identifiable{
    public var id = UUID()
}

extension ToDoItem {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<ToDoItem> {
        return NSFetchRequest<ToDoItem>(entityName: "ToDoItem")
    }

    @NSManaged public var title: String?
    @NSManaged public var toCheck: NSSet?
    public var checkList: [CheckItem]{
         let set = toCheck as? Set<CheckItem> ?? []
         return set.sorted(){$0.wrappedName > $1.wrappedName}
    }
    public var checkedItems: [CheckItem]{
         let set = toCheck as? Set<CheckItem> ?? []
        return set.filter({$0.checked}).sorted(){$0.wrappedName > $1.wrappedName}
    }

    override public func willChangeValue(forKey key: String) {
        super.willChangeValue(forKey: key)
        self.objectWillChange.send()
    }
}

// MARK: Generated accessors for toCheck
extension ToDoItem {

    @objc(addToCheckObject:)
    @NSManaged public func addToToCheck(_ value: CheckItem)

    @objc(removeToCheckObject:)
    @NSManaged public func removeFromToCheck(_ value: CheckItem)

    @objc(addToCheck:)
    @NSManaged public func addToToCheck(_ values: NSSet)

    @objc(removeToCheck:)
    @NSManaged public func removeFromToCheck(_ values: NSSet)

    static func allItemsFetchRequest() -> NSFetchRequest<ToDoItem>{
        let fetchRequest: NSFetchRequest<ToDoItem> = ToDoItem.fetchRequest()
        fetchRequest.sortDescriptors = []
        return fetchRequest
    }

}

@objc(CheckItem)
public class CheckItem: NSManagedObject, Identifiable {
    public var id = UUID()
}

extension CheckItem {
    override public func willChangeValue(forKey key: String) {
        super.willChangeValue(forKey: key)
        self.objectWillChange.send()
    }
    @nonobjc public class func fetchRequest() -> NSFetchRequest<CheckItem> {
        return NSFetchRequest<CheckItem>(entityName: "CheckItem")
    }

    @NSManaged public var name: String?
    public var wrappedName: String{
        name ?? "unnamed"
    }
    @NSManaged public var checked: Bool
    public var checkedTrigger: Bool{
        get{
            checked
        }
        set{
            if let todo = self.toDo {
                todo.willChangeValue(forKey: "checkedItems")
            }
            checked = newValue
        }
    }
    @NSManaged public var toDo: ToDoItem?
}

struct ContentView: View {

    @Environment(\.managedObjectContext) var moc
    @FetchRequest(fetchRequest: ToDoItem.allItemsFetchRequest()) var toDoItems: FetchedResults<ToDoItem>

    var body: some View {
        VStack{
            List{
                ForEach(self.toDoItems){toDoItem in
                    ToDoView(toDoItem: toDoItem)
                }
            }
            HStack{
                 Button("add test items"){
                    let toDo = ToDoItem(context: self.moc)
                    toDo.title = "some title"
                    let check1 = CheckItem(context: self.moc)
                    check1.name = "come item to Check"
                    check1.toDo = toDo
                    let check2 = CheckItem(context: self.moc)
                    check2.name = "another item to Check"
                    check2.toDo = toDo
                }
                Spacer()
                Button("save"){
                    if self.moc.hasChanges{
                        do{
                            try self.moc.save()
                        }catch{
                            print("cant save changes: error.localizedDescription")
                        }
                    }
                }
            }
        }
    }
}

struct ToDoView: View {
    @ObservedObject var toDoItem: ToDoItem
    var body: some View{
        VStack{
            Text(toDoItem.title!)
            Text("checked: \(toDoItem.checkedItems.count)/\(toDoItem.checkList.count)")
            ForEach(toDoItem.checkList){check in
                CheckView(checkItem: check)
            }

         }
    }
}

struct CheckView: View{
    @ObservedObject var checkItem: CheckItem
    var body: some View{
        HStack{
            Text(checkItem.name ?? "unnamed" )
            Toggle("checked:", isOn: $checkItem.checkedTrigger)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        return ContentView().environment(\.managedObjectContext, context)
    }
}