简体   繁体   English

我应该如何编辑我的JavaScript,以便能够单击html列表?

[英]How should I edit my javascript so that I have the ability to click through a html list?

I have a list that I would like users to click through using next and previous. 我有一个列表,希望用户使用下一个和上一个单击。 For some reason, my current code isn't working as it should. 由于某种原因,我当前的代码无法正常工作。 It's not doing anything. 它什么也没做。 When users select next, I would like for it to show the next item and vice versa. 当用户选择下一个时,我希望它显示下一个项目,反之亦然。

HTML: HTML:

{% extends "layout.html" %}
    {% block content %}
    <div class="jumbo">
      <div class ="results" align="center">
       <h1>Test </h1>
        <ul class = "food_choice" style="list-style: none;">
           <li><span>{{search_results}}<br/> Rating: {{search_results1}}<br/><a     href="{{search_results2}}"> Check out {{search_results}} on Yelp</a><br/>{{search_results3}}<br/>{{search_results4}}<br/>{{search_results5}}</span></li>
           <li><span>{{search_results6}}<br/> Rating: {{search_results7}}<br/><a href="{{search_results8}}"> Check out {{search_results6}} on Yelp</a><br/>{{search_results9}}<br/>{{search_results10}}<br/>{{search_results11}}</span>         </li>     
        </ul>
         <button class="next" type="button">Next</button>
         <button class="back" type="button">Back</button>
       </div>
    </div> 

{% endblock %}

Javascript: Javascript:

<script>
    $('.results').each(function() {

        $(this).find('li').first().siblings().hide();

        $(this).find('.next').click(function () {
            $(this)
                .parent('.results')
                .find('li:first-child')
                .fadeOut(function () {
                $(this)
                    .next()
                    .fadeIn()
                $(this)
                    .appendTo($(this).parent())
            });

        });

        $(this).find('.back').click(function () {
           $(this)
            .parent('.results')
            .find('li:food_choice')
            .fadeOut(function () {
                $(this)
                .parent()
                .find('li:last-child')
                .fadeIn()
                .prependTo($(this).parent())
            });
        });

    });
</script>

Something along the lines of this should work: 与此类似的东西应该起作用:

$(".results").each(function(){
    var results = $(this);
    results.find(".next").click(function(){
        results.find("li").each(function(){
            if($(this).hasClass("active") && $(this).next().length > 0)
            {
                $(this).removeClass("active");
                $(this).next().addClass("active");
            }
        });
    });
    results.find(".back").click(function(){
        results.find("li").each(function(){
            if($(this).hasClass("active") && $(this).prev().length > 0)
            {
                $(this).removeClass("active");
                $(this).prev().addClass("active");
            }
        });
    });
});

暂无
暂无

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

相关问题 我该怎么做才能编辑我的 vue 待办事项列表以添加项目? - How do I make it so that I can edit my vue to do list to add items? 如何在浏览器中编辑 javascript,就像我可以使用 Firebug 编辑 CSS/HTML 一样? - How can I edit javascript in my browser like I can use Firebug to edit CSS/HTML? 我应该如何命名我的javascript库函数,以便它们可以与普通的旧javascript区分开来? - How should I name my javascript library functions so they are differentiable from plain old javascript? 我的 JavaScript 代码以及 HTML 如何显示它有问题 - I have a problem with my JavaScript code and how HTML displays it 如何才能使我的API只能通过我的应用程序(Javascript和PHP)访问? - How would I make it so my API is only accessible through my application (Javascript and PHP)? 当我单击编辑图像时,我希望我的编辑功能能够正常工作 - I would like to have my edit function to work when i click on the edit image 我如何更改 html 或 javascript 代码,所以当我单击 div 时,即使它不起作用。我该如何解决? - How can i change html or javascript code, so when i click divs even it doesnt work .How can I fix it? 如何将HTML代码添加到我的网站,以便当人们单击它时,它与我打开WhatsApp对话? - How do I add an HTML code to my website so that when people click it, it opens a WhatsApp conversation with me? 如何添加在 React 组件中编辑文本的功能? - How do I add the ability to edit text within a react component? 我应该如何在我的文件中构建我的 axios.get,以免重复“无法读取未定义的属性(读取'0')? - How should I structure my axios.get in my file so to not have repeated "cannot read properties of undefined (reading '0')?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM