Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery图像错误处理_Javascript_Jquery - Fatal编程技术网

Javascript JQuery图像错误处理

Javascript JQuery图像错误处理,javascript,jquery,Javascript,Jquery,在我的网站上,我有一个JQuery自动完成。自动完成功能工作正常。我有来自查询的数据,按我希望的方式进行拉取和存储 这一切都可以看到。(源代码可以查看,Enjin让它很难看,但它就在那里。) 当前,返回列表的格式为,如果存在图片,则显示图片;如果不存在图片,则显示占位符 return $("<li>").append("<a>" + '<img class="imgThumb" onError="this.src=\'http://files.enj

在我的网站上,我有一个JQuery自动完成。自动完成功能工作正常。我有来自查询的数据,按我希望的方式进行拉取和存储

这一切都可以看到。(源代码可以查看,Enjin让它很难看,但它就在那里。)

当前,返回列表的格式为,如果存在图片,则显示图片;如果不存在图片,则显示占位符

return $("<li>").append("<a>" +
        '<img class="imgThumb" onError="this.src=\'http://files.enjin.com/450575/file_storage/scripts/crScripts/images/placeholder.jpg\';" src="http://census.daybreakgames.com/files/dcuo/images/character/paperdoll/' + item.desc.character_id + '" style="float:left; margin-right: 6px; max-height: 50px;width: auto;">' + '<span style="display:table-cell; vertical-align:middle; height:50px;">' + item.value + "<br>CR: " + item.desc.combat_rating + " " + item.desc.world.name + '</span>' + "</a>").appendTo(ul);
    }   
在存在图像的字符上,代码可以完美地工作。图像显示,世界上一切正常。(可以通过在trex中键入“查找字符:输入”来查看。)在没有图像的字符上,它只显示一个损坏的图像而不是我的占位符(通过在输入框中键入“场”来查看。请注意下拉列表中显示的徽标…这是占位符的较小版本。)

我是否错误地使用了.error?还是有其他方法来解决这个问题


编辑:根据回答,我回顾并查看了append函数以及jQuery的内置函数。我找到了.html,并且能够利用它来做我想做的事情。我不知道为什么一开始我不能让它工作,但感谢那些建议再次查看它的人。

我认为你应该在onError handle函数中使用转义字符,只需删除onError handle函数中的“\”,然后像这样更改First代码块:

return $("<li>").append("<a>" +
    '<img class="imgThumb" onerror="this.src='http://files.enjin.com/450575/file_storage/scripts/crScripts/images/placeholder.jpg';" src="http://census.daybreakgames.com/files/dcuo/images/character/paperdoll/' + item.desc.character_id + '" style="float:left; margin-right: 6px; max-height: 50px;width: auto;">' + '<span style="display:table-cell; vertical-align:middle; height:50px;">' + item.value + "<br>CR: " + item.desc.combat_rating + " " + item.desc.world.name + '</span>' + "</a>").appendTo(ul);
}  
返回$(“
  • ”)。追加(“”)+ “+”+“+item.value+”
    CR:“+item.desc.batch_rating+”+item.desc.world.name+”)。附录(ul); }
  • 希望这能有所帮助


    顺便说一句,虽然'onError'有效,但我认为正确的关键字是'onError'用于错误回调^ ^

    谢谢你的建议。您引用的第一个代码块工作正常。下拉返回中没有错误。问题在于如何让问题中的最后一个代码块正常工作。最后一个代码块在my Edge和Chrome的jquery 1.12.1中运行良好。既然第一个代码块中的“onerror”事件可以正常工作,为什么不使用这种方式呢?您使用的是jQuery,并且您认为在生成的元素上内联事件处理程序是一个好主意?考虑:
    $('').on(…您的代码在这里…).appendTo(您的链接)
    $("#crCharImage").error(function() {$("#crCharImage").attr( "src", "http://files.enjin.com/450575/file_storage/scripts/crScripts/images/placeholder_large.png");}).attr("src", "http://census.daybreakgames.com/files/dcuo/images/character/paperdoll/" + ui.item.desc.character_id);
    
    return $("<li>").append("<a>" +
        '<img class="imgThumb" onerror="this.src='http://files.enjin.com/450575/file_storage/scripts/crScripts/images/placeholder.jpg';" src="http://census.daybreakgames.com/files/dcuo/images/character/paperdoll/' + item.desc.character_id + '" style="float:left; margin-right: 6px; max-height: 50px;width: auto;">' + '<span style="display:table-cell; vertical-align:middle; height:50px;">' + item.value + "<br>CR: " + item.desc.combat_rating + " " + item.desc.world.name + '</span>' + "</a>").appendTo(ul);
    }