英文:
Detect android phone via jQuery for a tag
问题
// 用内联的 JavaScript/jQuery 在 HTML 文档中实现以下逻辑:
// 如果是安卓手机点击“返回首页”按钮,链接为"x.com",否则链接为"z.com"
$('.android').on('click', function() {
if (/Android/i.test(navigator.userAgent)) {
// 是安卓手机
$(this).attr('href', 'x.com');
} else {
// 不是安卓手机
$(this).attr('href', 'z.com');
}
});
英文:
I have this code in my html source
<body>
<a class="android">back to home page</a>
</body>
how can i use inline js/jquery code to say if android phone click "back to home page" button href= "x.com" else href= "z.com"
I want to use inline source js/jquery in my html document
答案1
得分: 0
<a class="android" id="android" >回到首页</a>
let anchor = document.getElementById("android");
if (screen.width <= 640) {
anchor.href="链接"
}
else{
anchor.href="链接2"
}
英文:
You can use the following code in
<a class="android" id="android" >back to home page</a>
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let anchor = document.getElementById("android");
if (screen.width <= 640) {
anchor.href="link"
}
else{
anchor.href="link2"
}
<!-- end snippet -->
答案2
得分: 0
<body>
<script type="text/javascript">
var isAndroid = / Android/i.test(navigator.userAgent.toLowerCase());
if (isAndroid) {
document.getElementById("abc").href = "https://stackoverflow.com/";
} else {
document.getElementById("abc").href = "https://link2";
}
</script>
</body>
英文:
<body>
<script type="text/javascript">
var isAndroid = / Android/i.test(navigator.userAgent.toLowerCase());
if (isAndroid) {
document.getElementById("abc").href = "https://stackoverflow.com/;
} else {
document.getElementById("abc").href = "https://link2";
}
</script>
</body>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论