我想在我的頁面頂部添加javascript,如果它檢測到移動瀏覽器,那麼我的html代碼將使用相應的html代碼和圖像高度和寬度設置。如何爲移動和桌面瀏覽器創建兩個不同的圖像?
<script type="text/javascript">
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))
<img src="https://drive.google.com/uc?export=view&id=0B2VHnnIsISjHdWRLVk9zS2VuUFk" alt="" style="width:1904px;height:328px;">
else
<img src="https://drive.google.com/uc?export=view&id=0B2VHnnIsISjHdWRLVk9zS2VuUFk" alt="" style="width:1904px;height:528px;">
</script>
我不知道爲什麼它不工作?
如何使這一全面的JavaScript
var img = document.createElement("img");
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
img.src = "http://home.bt.com/images/the-iphone-7-and-7-plus-which-bt-mobile-plan-is-right-for-me-136409622203503901-160915131847.jpg";
img.style.width = "648px";
img.style.height = "365px";
} else {
img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Desktop_computer_clipart_-_Yellow_theme.svg/281px-Desktop_computer_clipart_-_Yellow_theme.svg.png";
img.style.width = "281px";
img.style.height = "203px";
}
document.body.appendChild(img);
所以其在
<script type="text/javascript">
一些代碼
像這樣完整的代碼,所以我可以把它添加到我的HTML代碼
編輯新
這是正確的嗎?
<script type="text/javascript">
var img = document.createElement("img");
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
img.src = "https://drive.google.com/uc?export=view&id=0B2VHnnIsISjHdWRLVk9zS2VuUFk";
img.style.width = "1904px";
img.style.height = "365px";
} else {
img.src = "https://drive.google.com/uc?export=view&id=0B2VHnnIsISjHdWRLVk9zS2VuUFk";
img.style.width = "1904px";
img.style.height = "403px";
}
document.body.appendChild(img);
</script>
您無法在JavaScript中編寫HTML標籤。請參閱@Yusaf Khaliq答案。 –