我想在我的页面顶部添加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答案。 –