我想绑定img与函数的onerror。图像标记jQuery中像这样创建绑定onError与动态创建的图像标记在jquery
this._img1 = $("<img>");
这是功能
function imgError(image) {
image.onerror = "";
image.src = '/Content/img/placeholder-profile.jpg';
return true;
}
现在我想这个功能绑定的onerror ... 我已试过的东西,他们不会工作我
this._img1.on("onError", "imgError(" + this._img1 + ")");
和
this._img1.prop("onError", "imgError(this)");
和
this._img1.error(function() {
_img1.src = '/Content/img/placeholder-profile.jpg';
});
任何人都可以建议我怎么能实现呢?
什么是'this._img1'?我想这是一个DOM节点元素,而不是jq对象。所以你必须把它包装在jq对象中以使用jq方法:'$(this._img1).on('error',function(){_ img1.src ='/Content/img/placeholder-profile.jpg' ;});'。或者使用本机js方法绑定事件 –
@A。 Wolff 'this'是其中包含所有其他元素并显示结果的主要div元素。这实际上是jquery小部件调用。 –