简体   繁体   English

的Java语言更改效果 <ul><li> 工作不正常

[英]Javascript change effect for <ul><li> is not working correctly

I am new to Javascript. 我是Java语言的新手。 I want to click on the Navi bar to change some effect(underline to the words) 我想点击导航栏以更改某些效果(在文字上加下划线)

Coding for HTML HTML编码

<div class="nav" id="nav">
    <ul>
        <li><a href="index.html" class="underline_text" onclick="SetTabState(this)">Home</a></li>
        <li><a href="index.html#text1" onclick="SetTabState(this)">text1</a></li>
        <li><a href="index.html#text2" onclick="SetTabState(this)">text2</a></li>
    </ul>
</div>

<div id="text1"> <!--> jump to here<-->
       .....
</div>

<div id="text2"> <!--> jump to here<-->
       .....
</div>

<script>
    function SetTabState(dom){
         $("#nav ul li").find('.underline_text').removeClass('underline_text');
         $(dom).addClass('underline_text');
    }   
</script>

在此处输入图片说明

Just like the screenshot, when clicking "text1" tab, the home underline is removed and "text1" underline is added... 就像屏幕截图一样,单击“ text1”选项卡时,将删除主页下划线,并添加“ text1”下划线...

However, for my coding, when clicking on "text1", the underline is not changing and remain at "home". 但是,对于我的编码,单击“ text1”时,下划线不会更改,而是保持在“ home”。 Anyone knows the problem here? 有人知道这里的问题吗? Thx! 谢谢!

This is because from your html 这是因为从您的HTML

<a href="index.html#text2" onclick="SetTabState(this)">

this refer to <a> , so dom is actually an <a> 这是指<a> ,因此dom实际上是<a>

From your SetTabState(dom) , dom suppose to be a <li> ? 从您的SetTabState(dom) ,dom假设是<li>吗?

You can add a event delegation to the <ul > with delegate to the <li> , like 您可以将事件委托添加到<ul >中,而将委托添加到<li> ,例如

 function SetTabState(dom) { $("#nav ul li").find('.underline_text').removeClass('underline_text'); $(dom).addClass('underline_text'); } $(document).ready(function() { $("#nav ul").on('click', 'li', function() { SetTabState(this) }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="nav" id="nav"> <ul> <li><a href="#" class="underline_text">Home</a></li> <li><a href="#text1">text1</a></li> <li><a href="#text2">text2</a></li> </ul> </div> <div id="text1"> <!-->jump to here <--> ..... </div> <div id="text2"> <!-->jump to here <--> ..... </div> 

As others have said, the dom variable refers to the <a> element, but you're trying to remove the class from the <li> . 就像其他人所说的那样, dom变量引用了<a>元素,但是您正在尝试从<li>删除该类。 Instead of using .find , you can directly select the element with the underline_text class: 除了使用.find ,您还可以直接使用underline_text类选择元素:

<script>
    function SetTabState(dom){
         $("#nav ul li .underline_text").removeClass('underline_text');
         $(dom).addClass('underline_text');
    }   
</script>

Because you need to change the state of the other elements rather than the only element you clicked, I suggest you use a function for the whole group of elements instead of handle click event on each element. 由于您需要更改其他元素的状态,而不是单击的唯一元素,因此建议您对整个元素组使用一个函数,而不要对每个元素处理click事件。 This is my code: 这是我的代码:

function initClickableNav(nav) {
    var navItems = nav.querySelectorAll('a');
    var activeClass = 'underline_text';
    nav.addEventListener('click', function (event) {
        [].forEach.call(navItems, function (navItem) {
            if (navItem === event.target || navItem.contains(event.target)) {
                navItem.classList.add(activeClass);
            } else {
                navItem.classList.remove(activeClass);
            }
        });
    });
}

initClickableNav(document.querySelector('#nav'));

HTML HTML

<div class="nav" id="nav">
    <ul>
        <li><a href="index.html" class="underline_text"><span>Home</span></a></li>
        <li><a href="index.html#text1">text1</a></li>
        <li><a href="index.html#text2">text2</a></li>
    </ul>
</div>

You can do like this :- 您可以这样:-

 function SetTabState(dom){ $("#nav ul li a").removeClass('underline_text'); $(dom).addClass('underline_text'); } 
 ul { padding: 0; margin: 0; list-style: none; } ul li a { text-decoration: none; } ul li a.underline_text { border-bottom: 1px solid #000; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="nav" id="nav"> <ul> <li><a href="#" class="underline_text" onclick="SetTabState(this)">Home</a></li> <li><a href="#" onclick="SetTabState(this)">text1</a></li> <li><a href="#" onclick="SetTabState(this)">text2</a></li> </ul> </div> 

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

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