2 回答

TA貢獻1825條經驗 獲得超4個贊
需要考慮的幾點:
服務器端解決方案是不合適的:您想在客戶端測試資源可用性,而這只能通過客戶端作為代理來實現。
檢查資源可能需要時間,特別是比在客戶端呈現 html 所需的時間更多。在檢索資源失敗的情況下刪除可見標記(例如
span
示例的元素)可能會導致呈現的材料短暫顯示并再次消失,而意圖根本不顯示它。因此更好的策略是默認隱藏它并在確定資源可用性后立即顯示它。
function ready () {
let anl_toTest = Array.from ( document.querySelectorAll ( '.fa-sign-x' ) )
;
anl_toTest.forEach ( (e_span) => {
let myRequest = new Request ( e_span.parentNode.href)
;
// Fetch API. See [this MDN section](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for details and code samples
fetch(myRequest)
.then (
function(response) {
if (response.ok) { // Resource was successfully retrieved, show the link
e_span.style.display = "inline";
}
}
, function (e) {
console.log(`Failed on '${myRequest.url}': ${e.message}.`);
}
);
});
} // ready
window.addEventListener ( 'load', ready );
.fa-sign-x {
display: none;
}
<a href="https://www.gravatar.com/avatar/00000000000000000000000000000000?d=identicon&f=y" data-lightbox="kumas" data-title="Pattern" class="tab active" >
<span class="fa-sign-x" >Pattern</span>
</a>
<a href="https://www.example.com/whatever.jpg" data-lightbox="kumas" data-title="Failed" class="tab active" >
<span class="fa-sign-x" >Failed</span>
</a>
<a href="https://secure.gravatar.com/avatar/1a0ea6b0656f0af322419bd6a607598f?s=100&d=retro&r=g" data-lightbox="kumas" data-title="Polar Bear" class="tab active" >
<span class="fa-sign-x" >Polar Bear</span>
</a>

TA貢獻1850條經驗 獲得超11個贊
您可以使用file_exists()函數來檢查文件是否存在。
if(file_exists("/sunsetplanlama/".$sonuc['proje_ismi']."/kumas_1.png")){ ????echo?'<a?href="/sunsetplanlama/'.$sonuc['proje_ismi'].'/kumas_1.png"??data-lightbox="kumas"?data-title="'.$sonuc['proje_ismi'].'"?class="tab?active"?>K1</a>'; }
- 2 回答
- 0 關注
- 108 瀏覽
添加回答
舉報