簡體   English   中英

在此代碼中使用$(this)代替$('。article')無效嗎?

[英]Using $(this) instead $('.article') in this code didn't work?

var main = function(){
$('.article').click(function(){
    //$('.article').removeClass('current');
    $(this).removeClass('current');
    $('.description').hide();

    $(this).addClass('current');
    $(this).children('.description').show();
});
$('.article').dblclick(function(){
    //$('.description').hide();
    $(this).children('.description').hide();            
});

}; $(document).ready(main);

這段代碼是這樣的表格原型:http: //www.codecademy.com/courses/web-beginner-zh-4hxyb/0/5?content_from=make-an-interactive-website%3Ajquery-events單擊fullcreen您會看到使用$(this).removeClass('current')代替$('。article')。removeClass('current')。 此引用'article'類的關鍵字為何使用不引用'article'類的關鍵字,而必須使用'.artilce'。 有人可以解釋一下,謝謝!

在這里,您有一個簡化的代碼顯示了它的工作原理

var main = function(){
$('.article').click(function(){
    // below will change all with class article to green
    // $('.article').addClass('green')

    // with this it only changes the clicked one to green
    //$(this).addClass('green');

    // below will remove red-class from clicked .article
    //$(this).removeClass('red');

    //below will remove red-class from all elements with .article
    $('.article').removeClass('red');
});

}; $(document).ready(main);

http://jsfiddle.net/tgLckfgz/2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM