简体   繁体   English

无法让JS定位到特定目标

[英]Can't get JS to target specific li

I'm not a programmer. 我不是程序员。 I can read and understand some code, and I can google for answers. 我可以阅读和理解一些代码,也可以在Google上找到答案。 But this one escapes me. 但是这个逃脱了我。 I built a test page to try my hand at coding for mobile devices - http://www.stovebolt.com/testing2.html 我建立了一个测试页来尝试为移动设备编码-http: //www.stovebolt.com/testing2.html

At 800 pixels, the navigation stack on the left is hidden and replaced with a dropdown menu that uses a list structure with a hover function. 在800像素处,左侧的导航堆栈被隐藏并被下拉菜单取代,该下拉菜单使用具有悬停功能的列表结构。 This will work on mobile devices if the href for the parent li is #. 如果父级li的href为#,这将在移动设备上起作用。 However, you have to reload the page to get it to close the menu. 但是,您必须重新加载页面才能使其关闭菜单。

So I cast about for a solution and came across this: http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly 因此,我寻求解决方案并遇到了这个问题: http : //osvaldas.info/drop-down-navigation-sensitive-and-touch-friendly

I'm using his DoubleTapToGo javascript, and it works, but it applies to all the elements not just the parent li. 我正在使用他的DoubleTapToGo javascript,它可以工作,但它不仅适用于父级li,还适用于所有元素。 According to his page you just need to use the following to only have the parent require a double tap: 根据他的页面,您只需要使用以下内容即可让父项需要双击:

$( '#nav li:has(ul)' ).doubleTapToGo();

I'm not using the id #nav. 我没有使用ID #nav。 I'm using the class .mobile, so I replaced #nav with .moblie. 我使用的是.mobile类,所以我将#nav替换为.moblie。 But you have to double tap ALL the links, not just the dropdown. 但是您必须双击所有链接,而不仅仅是下拉列表。

I've tried negating the function, but that didn't work. 我试过否定函数,但这没有用。 I tried changing to li:hasClass() and created a new class for that one li. 我尝试更改为li:hasClass()并为那个li创建了一个新类。 Didn't work. 没用 How can I get the doubleclick to only work on the parent li? 我如何才能使Doubleclick仅在父级li上起作用? The code li:has(ul) should work, because there's only one sublist. 代码li:has(ul)应该可以工作,因为只有一个子列表。 But it doesn't. 但事实并非如此。

I can't help but think I'm missing something very simple. 我忍不住想想我缺少一些非常简单的东西。 Here's the code on my page, which I copied directly from the source of his webpage: 这是我页面上的代码,我直接从他的网页源代码中复制了这些代码:

<script>
    $( function()
    {
            $( '.mobile li:hasClass('mobileParent')').DoubleTapToGo();
    });

This is the html for the mobile list: 这是移动列表的html:

<div class="mobile" role="navigation">
<ul>
<li><a href="<!--#echo var="DOCUMENT_URI" -->" aria-haspopup="true">Around the 'Bolt...</a>
<ul>
<li><a href="http://www.stovebolt.com/">Home</a></li>
<li><a href="http://www.stovebolt.com/search.html">Search</a></li>
<li><a href="http://www.stovebolt.com/ubbthreads/ubbthreads.php">Discussion Forums</a></li>
<li><a href="http://www.stovebolt.com/gallery">Gallery</a></li>
<li><a href="http://www.stovebolt.com/techtips/">Tech Tips</a></li>
<li><a href="http://www.stovebolt.com/links">Links</a></li>
<li><a href="http://www.stovebolt.com/events">Events</a></li>
<li><a href="http://www.stovebolt.com/ubbthreads/ubbthreads.php/category/3/Swap_Meet.html">Swap Meet</a></li>
<li><a href="http://www.stovebolt.com/faq/">FAQs</a></li>
<li><a href="http://www.stovebolt.com/features">Features</a></li>
<li><a href="http://www.cafepress.com/mystovebolt" target="_blank">Stovebolt Hoo-ya</a></li>
<li><a href="http://www.stovebolt.com/office">Stovebolt Office</a></li>
</ul>
</li>
</ul>
</div>

Please try this 请尝试这个

$( function()
{
        $( '.mobile li.mobileParent').DoubleTapToGo();
});

But in you html there is no li with the class mobileParent . 但是在您的html中,类mobileParent不存在li So $( '.mobile li.mobileParent').DoubleTapToGo(); 因此$( '.mobile li.mobileParent').DoubleTapToGo(); wont work. 不会工作。 Add that calss and then use this code. 添加该校准,然后使用此代码。

This is how I solved the problem. 这就是我解决问题的方式。 I ripped out all the double-click stuff (since it was giving me errors), changed the link in the primary LI to "#" and added another LI to the dropdown menu to close it: 我删除了所有双击的东西(因为它给了我错误),将主LI中的链接更改为“#”,并在下拉菜单中添加了另一个LI以将其关闭:

<li><a href="#" aria-haspopup="true">Around the 'Bolt...</a> <ul> <li><a href="<!--#echo var="DOCUMENT_URI" -->">Close this Menu</a></li>

That just left one problem. 这只是一个问题。 Apparently Apple tries to emulate the hover event, so it requires a double click on dropdown menu items. 显然,Apple试图模拟悬停事件,因此它需要双击下拉菜单项。 So I added this script to over come that: http://cssmenumaker.com/blog/solving-the-double-tap-issue-on-ios-devices 因此,我在以下脚本中添加了该脚本: http : //cssmenumaker.com/blog/solving-the-double-tap-issue-on-ios-devices

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

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