预览图像Jquery脚本don't运行良好

Preview Image Jquery Script don't runs well

本文关键字:运行 图像 Jquery 脚本 don      更新时间:2023-09-26

我使用的Jquery脚本可以在鼠标悬停时预览图像,但我遇到的问题是,该脚本只在前两个图像中运行。

要访问我实现的web:http://81.35.152.41:8888/index.php/ca/dinamic/coleccions

我认为问题是因为我打印的是dejquery脚本中的html代码,因为我在jquery中使用Ajax和Json。

这是打印html代码的Jquery:

(function($) { 
    $(document).ready(function(){
        id=$("#colecciochange option:first").val()
        getcoleccions(id);
            //getpuntosdeventa(1);
                $("#colecciochange").change(function(){
                 getcoleccions($(this).val())
            });
            function getcoleccions(id)
            {
            $("ul.appends").empty()
            $("div.descripcio").empty()

                    $.getJSON('<?php echo url_for('ajax/coleccio/?id=',true)?>'+id, function(data) {

                          $.each(data.items, function(key,val) {
                                //items.push('<p>' + val.nombre + ','+val.direccion + ','+val.ciutad +'</p>');
                                //$("ul.appends").append('<li><a href=' +val.foto_g + ' class="preview" title><img src='+val.foto_th +' alt="prova" /></a></li>');
                                $("#galeria").append('<li><a href="/1.jpg" id="1" class="preview"><img src="/1s.jpg" alt="gallery thumbnail" /></a></li>');
                          });
                          $("div.descripcio").append('<p>' +data.nom_coleccio + '</p>');
                }); 
            }
        });
 })(jQuery)

这是进行图像预览的脚本:

this.imagePreview = function(){ 
    /* CONFIG */
        xOffset = 10;
        yOffset = 30;
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result
    /* END CONFIG */
  console.log($(this).attr("id"));
    $("a.preview").hover(function(e){
        console.log($(this).attr("id"));
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                                var id=($(this).attr("id"));

        $("#preview")
            .css("top",400 + "px")
            .css("left",150 + "px")
            .fadeIn("fast");                    
   },
    function(){
        this.title = this.t;    
        $("#preview").remove();
   });  
    /*$("a.preview").mousemove(function(e){
   console.log($(this).attr("id"));
        $("#preview")
            .css("top",400 + "px")
            .css("left",150 + "px")
    });*/
   $("a.preview").click(function(event) {
 event.preventDefault();
 //
});         
};
$(document).ready(function(){
    imagePreview();
});

问题出在什么时候?

感谢

问候

这是因为在元素插入页面之前,imagePreview函数正在运行,因此它们没有正确绑定。插入imagePreview();到$.getJSON回调函数中,就在这一行的正下方:

$("div.descripcio").append('<p>' +data.nom_coleccio + '</p>');

两件事:

  • 当文档准备好时,$("a.preview").hover(function(e){...}, function(e){...})将影响DOM中的元素,而不是之后使用JavaScript添加到DOM中的那些元素。改为使用$("#galeria").on("mouseenter", "a.preview", function(e){...}$("#galeria").on("mouseleave", "a.preview", function(e){...}
  • 您有具有相同id的HTML元素——id在整个HTML页面中应该是唯一的。这可能会导致问题,尤其是在IE中