最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find and remove class - Stack Overflow

programmeradmin1浏览0评论

I have the following structure:

<li id="0" class="instruction">
        <div class="row1">
            title
        </div>
        <div class="row2 details hidden">
            details
        </div>
    </li>

css for hidden is: display: none

I'd like when user clicked the instruction, then show the details div.

$(document).ready(function () {
        $(".instruction").click(function(e){

            $(e).find('.details').removeClass("hidden");


        });
    });

Tried to implement something like above, but doesn't work. any idea?

I have the following structure:

<li id="0" class="instruction">
        <div class="row1">
            title
        </div>
        <div class="row2 details hidden">
            details
        </div>
    </li>

css for hidden is: display: none

I'd like when user clicked the instruction, then show the details div.

$(document).ready(function () {
        $(".instruction").click(function(e){

            $(e).find('.details').removeClass("hidden");


        });
    });

Tried to implement something like above, but doesn't work. any idea?

Share Improve this question edited May 4, 2014 at 14:24 martynas 12.3k3 gold badges58 silver badges61 bronze badges asked May 4, 2014 at 14:10 JavaScripterJavaScripter 4,84210 gold badges39 silver badges45 bronze badges 1
  • 1 instead of e , use this, like this $(this).find('.details').removeClass("hidden"); – Amit Soni Commented May 4, 2014 at 14:11
Add a comment  | 

3 Answers 3

Reset to default 8

Off-answer, but ref for Googlers - vanilla Javascript option for finding a class and removing a class

// Finding all elements of a class (creates an array of results)
let x = document.getElementsByClassName("today");

// If it exists, remove it.
if(x.length > 0) { x[0].classList.remove("today"); }

You have written '.instructionRow' but as per the html it should be '.instruction'. And use $(this) to refer to the element that is clicked. And usage of 'on' (https://api.jquery.com/on/) is better...

$(document).ready(function () {
    $(".instruction").on('click', function(e){
        $(this).find('.details').removeClass("hidden");
    });
});
$(document).ready(function () {
    $(".instructionRow").click(function(){

        $(this).find('.details').removeClass("hidden");


    });
});
发布评论

评论列表(0)

  1. 暂无评论