문제

I've got this

<div id=results">
    <article>
        <span class="sel">Text Here</span>
    </article>
    <article>
        <span class="sel">Text Here</span>
    </article>
    <article>
        <span class="sel">Text Here</span>
    </article>
</div>

Got a jQuery to know wich "span" I've clicked:

$("#results").on('click', 'article .sel', function() {
    var index = $(this).index("#results article .sel");
});

If I would like to change that "span" class, how I could do it?

I thought about doing an .each() function for each "article", but I wouldn't know how to enter the "span" inside the .each() function.

May there is an easier way that I don't know.

Thanks for all.

도움이 되었습니까?

해결책 2

The clicked element is referenced by this so

$("#results").on('click', 'article .sel', function() {
    var index = $(this).index("#results article .sel");
    //to change the class
    $(this).addClass('newclass');//or do something
});

다른 팁

Just use removeClass() and addClass() (or even toggleClass()) to change the class of an element.

$(".sel").on("click", function()
{
    $(this).removeClass("sel").addClass("yournewclass");
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top