简体   繁体   English

如何在第一次触摸时打开下拉菜单,点击第二次 - Jquery 上的链接?

[英]How can I make a dropdown open on the first touch, follow the link on the second - Jquery?

I'm trying to get a dropdown menu to work correctly on touch based devices.我正在尝试让下拉菜单在基于触摸的设备上正常工作。 The menu opens fine on click, but because the parent element is also a link it then takes the user to the link before a dropdown item has been selected.菜单在单击时打开正常,但由于父元素也是一个链接,因此它会在选择下拉项之前将用户带到该链接。

Is there are way to make it so that (only on touch devices) the first tap of the parent element opens the dropdown and the second click brings you to the link?有没有办法让它(仅在触摸设备上)父元素的第一次点击打开下拉菜单,第二次点击将您带到链接?

I have a good knowledge of HTML and CSS but none really of javascript, so please be as descriptive as possible if the solution is script based.我对 HTML 和 CSS 有很好的了解,但对 javascript 一无所知,所以如果解决方案是基于脚本的,请尽可能具有描述性。

Thanks谢谢

Take a look at DoubleTapToGo:看看 DoubleTapToGo:

"When you tap the parent item for the first time, DoubleTapToGo prevents the browser from opening a new URL but allows that if tapped once again in succession." “当您第一次点击父项时,DoubleTapToGo 会阻止浏览器打开新的 URL,但如果连续再次点击,则允许打开。”

http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly

This may help.这可能会有所帮助。 if you have a link如果你有链接

<a href="#">your drop down menu</a> I mean add # to href <a href="#">your drop down menu</a>我的意思是将#添加到href

Alternatively do this in JavaScript或者在 JavaScript 中执行此操作

var link = $('.menu').find('a');

$(document).on('touchstart', link, function (e) {
    e.preventDefault();
    //do your stuff
});
function createMenu(menuName,menuTitle,menuEntries,left,width) {

 numEntries = menuEntries.length;

document.write('<div class="menuTitle" style="left:0px; width:100px;"');
document.write('onMouseover="menuToggle(\'' + menuName + '\');"');
document.write('onMouseout="menuToggle(\'' + menuName + '\');">');
document.write(menuTitle);
document.write('</div>');

     document.write('<div id="myMenu" class="menu" style="left:0px; width:100px;"');
     document.write('onMouseout="menuToggle(\'' + menuName + '\')">');
     for (i = 0; i < numEntries; i++) {
        document.write('<a href="' + menuEntries[i].url + '" class="menuEntry">' + menuEntries[i].entry + '</a><br>');
     }
     document.write('</div>');

}

  function menuToggle(target) {

     targetMenu = (document.getElementById) ? document.getElementById(target).style : eval("document." + target);
     targetMenu.top = (parseInt(targetMenu.top) == 22) ? -2000 : 22;

     }
  // -->
  </script>

this is JavaScript for creating a drop-down menu that drops when the mouse is hovered over it - it's from a book called JavaScript in 10 Simple Steps or Less by Arman Danesh.这是用于创建下拉菜单的 JavaScript,当鼠标悬停在下拉菜单上时,它会下降 - 它来自 Arman Danesh 的一本名为JavaScript in 10 Simple Steps or Less的书。 He's a great author and will probably explain this a lot better than me - but here goes;他是一位伟大的作家,可能会比我更好地解释这一点——但这里是;

what you're doing is creating a function with predefined variables as declared in the ().您正在做的是使用 () 中声明的预定义变量创建一个函数。 the line numEntries... sets the value of the variable numEntries as the length of predefined variable menuEntries. numEntries... 行将变量 numEntries 的值设置为预定义变量 menuEntries 的长度。 In other words, (in this example the number of entries programmed is three, making numEntries = 3) however many entries you code is the value of numEntries.换句话说,(在本例中,编程的条目数为 3,使得 numEntries = 3)但是您编码的许多条目是 numEntries 的值。

the document.write stuff may look confusing, but it's just javascript code which holds the HTML code, because just putting the div on it's own would break the program since you're putting this in script tags. document.write 的东西可能看起来很混乱,但它只是保存 HTML 代码的 javascript 代码,因为将 div 放在它自己的上面会破坏程序,因为您将它放在脚本标签中。 if you don't understand me at any point just ask and i'll tell you what's in the textbook when i have it on me later.如果你在任何时候不明白我的意思,只要问,我会告诉你课本上有什么,当我以后有课本时。

That's the first function dealt with, the second is what makes the menu dropdown on the mouse hover.这是处理的第一个功能,第二个功能是使鼠标悬停的菜单下拉菜单。 i don't know how to explain this clearly to you without quoting the book, but i'll try if you need me to explain it.如果不引用这本书,我不知道如何向您清楚地解释这一点,但是如果您需要我来解释,我会尝试。 I can give you the whole example if you need the HTML code for displaying the menu too, I have it on my computer.如果你也需要显示菜单的 HTML 代码,我可以给你整个例子,我的电脑上有它。

Hope this helps and please just ask if you need me to go over anything.希望这会有所帮助,请询问您是否需要我介绍任何内容。 I'll keep researching how to alter it for a touch screen for you x我会继续研究如何为你改变它的触摸屏 x

$('.has-sub').on("touchstart", function (e) {
 var submenu = $(this).find('ul').first();
 var link = $(this); //preselect the link
 if (link.hasClass('hover')) {
       link.removeClass('hover');
       $(submenu).slideUp(0);
       e.stopPropagation()
       return true;
 } else {
       $(submenu).slideDown(0);
       link.addClass('hover');
       e.stopPropagation();
       return false; //extra, and to make sure the function has consistent return points
 }

}); });

My solution is toggling the href content in javascript:我的解决方案是在 javascript 中切换 href 内容:

 // JS let dropdown = document.getElementsByClassName("dropdown") let droplink = document.getElementsByClassName("drop-link") //console.log(droplink[0].getAttribute('href')) to check code for (let i = 0;i<dropdown.length;i++){ // if you have more than one dropdown dropdown[i].addEventListener('click', function(){ // if dropdown is already displaying if(dropdown[i].classList.contains('YourdisplayDropdownClass')){ droplink[i].href = 'myhtmlpage.html' } else { droplink[i].href = '#' dropdown[i].classList.add('YourdisplayDropdownClass'); } }); };

Structure in HTML HTML 中的结构

 <nav> <ul> <li class="dropdown"><a class="drop-link" ref="myhtmlpage.html">MyHTMLpage</a> <ul class="drop-menu"> <li><a href="mysubpage1.html" class="drop-item">MySubpage1</a></li> <li><a href="mysubpage2.html" class="drop-item">MySubpage2</a></li> </ul> </li> </ul> </nav>

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

相关问题 如何在第一次点击时突出显示链接; 并在第二次点击时关注它,除非在其他地方点击,然后重新初始化非突出显示的状态? - How can I highlight a link on first click; and follow it on second click, unless clicked elsewhere, then reinitialize non-highlighted state? 如何使Telerik GridImageColumn跟随点击链接? - How can I make Telerik GridImageColumn follow a link on click? 在使用jQuery时如何让href跟随链接 - How can I get href to follow a link when using jQuery 如何从第一个弹出窗口打开第二个jquery-mobile弹出窗口 - How can I open a second jquery-mobile popup from the first popup 我如何制作锚标记来跟随链接(href),然后以编程方式在该链接上单击。 - how can i make an anchor tag to follow a link (href) and then make a click programatically on that link. 如何使第一个链接打开特定链接 - How to make the first link open a specific link 我如何使用jQuery.when()从第一次校准中进行第二次调用 - How to can i use jQuery.when() to make the second call from the first cal 经过一定时间后,我可以让浏览器跟随链接吗? - Can I make the browser follow a link after a certain amount of time with jQuery? Bootstrap Twitter Dropdown:让父母点击链接 - Bootstrap Twitter Dropdown: Make the parent follow the link 我怎样才能使jQuery范围滑杆变得友好? - How can i make a jQuery range slider touch friendly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM