.click()仅影响具有类的一个元素 [英] .click() affect only one element with class

查看:91
本文介绍了.click()仅影响具有类的一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了新闻列表,简短和全文,以及新闻报道的标题。点击它可以展开全视图,反之亦然。然而,点击第一篇文章也会扩展所有其他文章。我怎么只影响我点击的那个?

I've got the list of news, with short and full text, and a title of the news article. Clicking on it expands full view and vise versa. Yet clicking on first article also expands all other articles. How do I only affect the one I clicked?

以下是示例

Here's the example:

$(document).ready(function () {
    $('.news-expand').click(function () {
        $(".news-content-short").toggle();
        $(".news-content-full").toggle();
        $('.news-date').toggleClass('active-date');
        $('.news-expand').toggleClass('active');
    });
});


推荐答案

您只需要处理该部分中的项目你点击了。代码如下:

You need to act on only the items in the section you clicked. The code for that would be like:

$(document)
    .ready(function () {
    $('.news-expand')
        .click(function () {
        var par = $(this).parents('.main-news-holder');
        par.find(".news-content-short").toggle();
        par.find(".news-content-full").toggle();
        par.find('.news-date').toggleClass('active-date');
        par.find('.news-expand').toggleClass('active');
        return false;
    });
});

检查演示: http://jsbin.com/ikijap/1/edit

这篇关于.click()仅影响具有类的一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆