简体   繁体   English

jQuery $ this +同级元素调用不起作用

[英]jQuery $this + sibling element call not working

I'm not sure if my jQuery selectors are the problem or if it's my jQuery code. 我不确定我的jQuery选择器是问题还是我的jQuery代码。

I am trying to add a click event on <i> that when clicked it adds class to right below-fold-child-text making it appear from outside the screen and visible on the page. 我正在尝试在<i>上添加一个单击事件,当单击该事件时,它会将类添加到below-fold-child-textbelow-fold-child-text从而使其从屏幕外部显示并在页面上可见。

I'm wondering if I'm setting up my html all wrong, should <i> be inside the .below-fold-child-text div? 我想知道我是否完全错误地设置了html, <i>应位于.below-fold-child-text div中? Where do I find some good examples of this being implemented? 我在哪里可以找到一些很好的例子来实现这一目标? Is my jQuery selector all wrong? 我的jQuery选择器全都错了吗?

Thank you! 谢谢!

This is my jQuery code so far: 到目前为止,这是我的jQuery代码:

$(".fly-in").each(function(){
    $(this).on("click", function() {
    //    $(this).find(".below-fold-child-text").toggleClass(".left-show");
       $(".right .below-fold-child-text",this).toggleClass(".left-show");
    });
}); 

My Sass code: 我的Sass代码:

&-text {
        background: $orange;
        color: $white;
        position: absolute;
        width: 40%; 
        top: 0px;
        z-index: 4;
        height: 100%;
        padding: 30px;

        &.left {
            // transform: translateX(0);
               transform: translateX(-999999px);
              &-show {
                 transform: translateX(0px);
                }
             }

                &.right {
                    // transform: translateX(623px);
                    transform: translateX(99999px);
                    &-show {
                        transform: translateX(623px);
                    }
                }

                h3 {
                    text-transform: uppercase;
                    font-size: 130%;

                }
                p {
                    font-size: 110%;
                }

                h3,p {
                    margin: 0;
                }


            } 

My html code: 我的html代码:

<div class="below-fold-child">
  <img src="https://placehold.it/800x200">
  <div class="right below-fold-child-text">
             <h3>Title</h3>
             <p>Lorem Ipsum dolor content goes here</p>
         </div>
         <i class="right fly-in fa fa-times-circle" aria-hidden="true"></i>
     </div>

     <div class="below-fold-child">
         <img src="https://placehold.it/800x200">
         <div class="left below-fold-child-text">
             <h3>A short history of florida</h3>
             <p>The first people reached Florida before 10,000 BC. They were hunters and gatherers. At the time the world was in grip of an ice age and huge animals like mastodons roamed what is now Florida. After the end of the ice age shellfish were abundant and by 2,000 BC the people of Florida were making pottery. Ater 1,500 BC they also built burial mounds.</p>
         </div>
         <i class="left fly-in fa fa-times-circle" aria-hidden="true"></i>
    </div> 

PS: If there's a better way to have asked this question please let me know too. PS:如果有更好的方法问这个问题,请也告诉我。 I'm very new to StackOverflow. 我是StackOverflow的新手。

I think I may have found the solution! 我想我可能已经找到了解决方案! Thank you so much for everyone that responded and made edit suggestions. 非常感谢所有回答并提出编辑建议的人。 Helped me think outside the box and showed me what $("class", this) meant. 帮助我跳出框框,向我展示了$(“ class”,this)的含义。 Cheers! 干杯!

$(".below-fold-child").each(function(){
    var toSelect = $(".below-fold-child-text",this); 
    $(".toclick", this).on("click", function(){
        console.log(toSelect)
        $(toSelect).toggleClass("right-show")
    });
});

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

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