简体   繁体   English

jQuery切换文本和图像不起作用

[英]jQuery toggle text and image not working

I am creating a dropdown accordion menu with jQuery. 我正在使用jQuery创建下拉式手风琴菜单。 I have worked on a few versions before and my script can work well before I add the image toggle. 之前,我已经开发了多个版本,并且在添加图像切换之前,脚本可以正常工作。

But after I have added the image toggle script, the whole toggle is not functioning well. 但是,在我添加了图像切换脚本之后,整个切换无法正常运行。

 $(function() { $('.showmore').click( function() { $(this).closest("div").next(".dropdown").toggle(); $(this).find('i').toggleClass('arrow-down arrow-up'); if ($(this).text() == "Show") { $(this).text("Hide"); } else { $(this).text("Show"); } $(this).closest("div").next(".dropdown").toggleClass('selected'); } ) }) 
 .title { background-color: #CCCCCC; } .sprite { background-image: url(https://image.ibb.co/bHTF8R/pinkarrow.png); background-repeat: no-repeat; display: block; } .arrow-down { width: 9px; height: 6px; background-position: -9px 0; } .arrow-up { width: 9px; height: 6px; background-position: 0 0; } .arrow-down, .arrow-up { display: inline-block; padding-left: 5px; } .dropdown { display: none; } .selected { border: 4px solid #CCCCCC; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="content"> <div class="title"> <span class="showmore">Show<i class="sprite arrow-down"></i></span> </div> <!--End of class Title--> <div class="dropdown"> XXXXXXXXXX Content XXXXXXXXXX </div> </div> 

So what I want to do is that: Originally the content will be hidden, with a title "Show" with arrow down icon is shown. 因此,我想做的是:最初将隐藏内容,并显示标题为“ Show”和向下箭头的图标。 If I click on the title, the hidden content will be shown and the title will change to "Hide" and with arrow up icon. 如果单击标题,将显示隐藏的内容,标题将变为“隐藏”并带有向上箭头图标。

Originally I can achieve the toggle with those text, however, after I have tried to add the image toggle script, it is not working. 最初,我可以使用这些文本来实现切换,但是,在尝试添加图像切换脚本后,它不起作用。

$(this).find('i').toggleClass('arrow-down arrow-up');

Anyone can share with me your solution/opinion here? 有人可以在这里与我分享您的解决方案/意见吗? Thank you so much for your time of reading my question! 非常感谢您阅读我的问题!

$(this).closet("div")

应该:

$(this).closest("div")

When you do this: 执行此操作时:

$(this).text("any text");

You remove the <i> element that you're using as an arrow. 删除用作箭头的<i>元素。 The resulting markup becomes: 结果标记变为:

<span class="showmore">any text</span>

Since the <i> is now gone, there's nothing to toggle. 由于<i>现在不存在,因此没有任何可切换的内容。 Instead of setting the text in the entire element, create a child element to hold the text: 与其在整个元素中设置文本,不如创建一个子元素来保存文本:

<span class="showmore">
    <span>Show</span>
    <i class="sprite arrow-down"></i>
</span>

Then you can target that specific element to set the text: 然后,您可以定位该特定元素以设置文本:

$(this).find("span").text("any text");

Demo: 演示:

 $(function() { $('.showmore').click( function() { $(this).closest("div").next(".dropdown").toggle(); $(this).find('i').toggleClass('arrow-down arrow-up'); if ($(this).find("span").text() == "Show") { $(this).find("span").text("Hide"); } else { $(this).find("span").text("Show"); } $(this).closest("div").next(".dropdown").toggleClass('selected'); } ) }) 
 .title { background-color: #CCCCCC; } .sprite { background-image: url(https://image.ibb.co/bHTF8R/pinkarrow.png); background-repeat: no-repeat; display: block; } .arrow-down { width: 9px; height: 6px; background-position: -9px 0; } .arrow-up { width: 9px; height: 6px; background-position: 0 0; } .arrow-down, .arrow-up { display: inline-block; padding-left: 5px; } .dropdown { display: none; } .selected { border: 4px solid #CCCCCC; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="content"> <div class="title"> <span class="showmore"> <span>Show</span> <i class="sprite arrow-down"></i> </span> </div> <!--End of class Title--> <div class="dropdown"> XXXXXXXXXX Content XXXXXXXXXX </div> </div> 

(Note: In my browser the styling on the "up arrow" doesn't look quite right. Probably needs some tweaking.) (注意:在我的浏览器中,“向上箭头”上的样式看起来不太正确。可能需要进行一些调整。)

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

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