[英]Setting img tag's src as the result of a javascript function
我有一個函數getImgurUrl(),我希望它返回的是img標簽的src值。
我怎樣才能做到這一點?
我的功能:
function getImgurUrl() {
//get imgur url for current image
var cookieArray = document.cookie.split(";");
var encodedURL = cookieArray[2].split("=");
var decodedURL = decodeURIComponent(encodedURL[1]);
return decodedURL;
}
和img標簽:
<img id="image" name="image" src="" alt="If you're seeing this something is wrong.">
window.onload = function() {
document.getElementById("image").src = getImgurUrl();
};
function getImageUrl(imageId) {
return document[imageId].src;
}
<img id="mike" src="http://yoursite.com/images/1.jpg" />
我還沒有測試過,但是您知道了。
我不確定您對Cookie邏輯的處理方式,因此請保持示例簡單。
-
我建議您添加jQuery來處理跨瀏覽器的兼容性,因為getElementById或其他元素可能無法始終工作,並且所有版本都不能正常工作,並且也有文檔說明。 可能不是很好,有時是window.functionName()
在jQuery中,您將類似地引用圖像src,但在我的回報中,您將使用id連成一個英鎊符號:
return $('#' + id).src;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.