使用jQuery检测Android手机的a标签

huangapple go评论93阅读模式
英文:

Detect android phone via jQuery for a tag

问题

  1. // 用内联的 JavaScript/jQuery 在 HTML 文档中实现以下逻辑:
  2. // 如果是安卓手机点击“返回首页”按钮,链接为"x.com",否则链接为"z.com"
  3. $('.android').on('click', function() {
  4. if (/Android/i.test(navigator.userAgent)) {
  5. // 是安卓手机
  6. $(this).attr('href', 'x.com');
  7. } else {
  8. // 不是安卓手机
  9. $(this).attr('href', 'z.com');
  10. }
  11. });
英文:

I have this code in my html source

  1. <body>
  2. <a class="android">back to home page</a>
  3. </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

  1. <a class="android" id="android" >回到首页</a>
  1. let anchor = document.getElementById("android");
  2. if (screen.width <= 640) {
  3. anchor.href="链接"
  4. }
  5. else{
  6. anchor.href="链接2"
  7. }
英文:

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 -->

  1. let anchor = document.getElementById(&quot;android&quot;);
  2. if (screen.width &lt;= 640) {
  3. anchor.href=&quot;link&quot;
  4. }
  5. else{
  6. anchor.href=&quot;link2&quot;
  7. }

<!-- end snippet -->

答案2

得分: 0

  1. &lt;body&gt;
  2. &lt;script type=&quot;text/javascript&quot;&gt;
  3. var isAndroid = / Android/i.test(navigator.userAgent.toLowerCase());
  4. if (isAndroid) {
  5. document.getElementById(&quot;abc&quot;).href = &quot;https://stackoverflow.com/&quot;;
  6. } else {
  7. document.getElementById(&quot;abc&quot;).href = &quot;https://link2&quot;;
  8. }
  9. &lt;/script&gt;
  10. &lt;/body&gt;
英文:
  1. &lt;body&gt;
  2. &lt;script type=&quot;text/javascript&quot;&gt;
  3. var isAndroid = / Android/i.test(navigator.userAgent.toLowerCase());
  4. if (isAndroid) {
  5. document.getElementById(&quot;abc&quot;).href = &quot;https://stackoverflow.com/;
  6. } else {
  7. document.getElementById(&quot;abc&quot;).href = &quot;https://link2&quot;;
  8. }
  9. &lt;/script&gt;
  10. &lt;/body&gt;

huangapple
  • 本文由 发表于 2023年2月19日 18:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定