简体   繁体   English

想要在检测到移动设备时使用javascript将菜单URL转换为#

[英]Want to convert a menu url to a # using javascript when a mobile device is detected

I have a drop down superfish menu in my responsive site and while I have the menu changing to a mobile friendly sidr slide menu for most mobile devices the client would still like to have the orginal dropdown menu showing when a tablet is in landscape orientation. 我在响应站点​​中有一个下拉的superfish菜单,而对于大多数移动设备,该菜单都已更改为适合移动设备使用的sidr幻灯片菜单,客户端仍然希望原始平板电脑下拉菜单在平板电脑处于横向时显示。

This obviously creates the usual hover issue with touch devices especially since we have the top li (of a drop down) also providing a href. 显然,这会导致触摸设备出现通常的悬停问题,特别是因为我们(下拉菜单中的)顶部li也提供了href。

My thoughts were that for those particular top level li's we'd add a class on which we could then run some javascript to replace the usual url link with a # when a mobile device is detected. 我的想法是,对于那些特定的顶级li,我们将添加一个类,当检测到移动设备时,可以在该类上运行一些javascript以#替换通常的url链接。 That would make the top li a touch item instead of a hover link item. 这将使顶部li成为触摸项,而不是悬停链接项。

Anyway below is what I have constructed to date and I had though this would work but so far I can't seem to get it to replace the original url with the # 无论如何,以下是我到目前为止构造的内容,尽管可以运行,但到目前为止,我似乎无法用#代替原始网址。

<script>
    $(document).ready(function($){
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
    if (agentID) {
        $('#sf-menu.sf-menu li.idevice a').attr('href', '#');
    }
});
</script>

I'm guessing I'm missing something really obvious but I've got to that point where I've exhausted my knowledge level - I'm not really a coder, more an enthusiastic tinkerer :). 我猜想我确实缺少一些明显的东西,但是我已经耗尽了我的知识水平-我不是真正的编码人员,更不是一个热心的修补匠:)。

Below is a snippet of the menu code for reference. 以下是菜单代码的片段,以供参考。

<div id="mateen">   
  <ul id="sf-menu" class="sf-menu">  
    <li  class="menu-item"><a href="/Home.htm">Home</a></li>
    <li  class="idevice"><a href="/advantages.htm">Advantages</a>
      <ul>
        <li  ><a href="/advantages.htm">Mateenbar Advantages</a></li>
        <li  ><a href="/superior-performance.htm">Superior Performance</a></li>
        <li  ><a href="/research-and-testing.htm">Research and Testing</a></li>
        <li  ><a href="/FAQ.htm">Frequently Asked Questions</a></li>
        <li  ><a href="/middle-east.htm">Middle East</a></li>
      </ul>
    </li>
    <li  class="idevice"><a href="/mateenbar.htm">Products &amp; Specifications</a>
      <ul>
        <li  ><a href="/mateenbar.htm">MateenBar</a></li>
        <li  ><a href="/mateendowel.htm">MateenDowel</a></li>
        <li  ><a href="/mateenbolt.htm">MateenBolt</a></li>
      </ul></li>
    <li  class="idevice"><a href="/corrosion-and-marine.htm">Applications &amp; Projects</a>
      <ul>
        <li  ><a href="/corrosion-and-marine.htm">Corrosion &amp; Marine</a></li>
        <li  ><a href="/roads-and-transportation.htm">Roads &amp; Transportation</a></li>
        <li  ><a href="/tunnelling-and-cutting.htm">Tunnelling &amp; Cutting</a></li>
        <li  ><a href="/electro-magnetic.htm">Electro-magnetic</a></li>
        <li  ><a href="/projects.htm">Projects</a></li>
      </ul>
    </li>
    <li  ><a href="/downloads.htm">Downloads</a></li>
    <li  class="idevice"><a href="/contacts.htm">Contacts</a>
      <ul>
        <li  ><a href="/contacts.htm">Locations</a></li>
        <li  ><a href="/about-pultron.htm">About Pultron</a></li>
      </ul>
    </li>
  </ul>
</div>

Any advice or help accepted at this point - hope I've provided enough relevant information. 目前接受的任何建议或帮助-希望我提供了足够的相关信息。

Thanks 谢谢

The problem is with the selector. 问题出在选择器上。 Change the following statement 更改以下语句

$('#sf-menu.sf-menu li.idevice a').attr('href', '#'); $('#sf-menu.sf-menu li.idevice a')。attr('href','#');

to

$('#sf-menu > li > a').attr("href","#") $('#sf-menu> li> a')。attr(“ href”,“#”)

For your reference: https://css-tricks.com/child-and-sibling-selectors/ 供您参考: https : //css-tricks.com/child-and-sibling-selectors/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM