工做中遇到要實現點擊圖片查看的功能,從網上找了一段js代碼,能夠用。ide
<img src="/pic/${pictureCertificate}" alt="憑證" width="100" height="100" id = "pictureCertificate" onerror="hidepic()"> function AddImgClickEvent() { var objs = document.getElementsByTagName("img"); for(var i=0;i<objs.length;i++) { objs[i].onclick=function() { window.open(this.src); } objs[i].style.cursor = "pointer"; } } AddImgClickEvent();
效果圖:
this
記錄一下工做時遇到的問題,方便下次使用!code