下面的 javascript 函数代码中“$(this)”包含什么?

What does '$(this)' contains in a below javascript function code?

函数代码如下:

$Core.notification =
{   
    bDebug: false,

    update: function()
    {
        setTimeout('$.ajaxCall("notification.update", "", "GET");', 1000);
    },

    setTitle: function()
    {
        var iTotal = 0;
        var sTitle = $('title').html();

        $('.holder_notify_count').each(function(){ alert($(this));
            iTotal += parseInt($(this).html());
        });

        var newTitle = '';      
        var aMatches = sTitle.match(/(\([0-9]*\))/i);
        if (aMatches !== null && isset(aMatches[1])){
            if (iTotal > 0){
                newTitle = '(' + iTotal + ') ' + sTitle.replace(aMatches[1], '');
                //$('title').html(newTitle.replace('#',''));
                // document.title = newTitle.replace('#', '');
            }
            else{
                //$('title').html(aMatches[1].replace('#',''));
                // document.title = aMatches[1].replace('#', '');
            }
        }
        else{
            if (iTotal > 0){
                //$('title').prepend('(' + iTotal + ') '); // it doesnt work in IE8
                // ie8 doesnt like hashes           
                var NewTitle = document.title.replace('#','');              
                // document.title = '(' + iTotal + ') ' + NewTitle;

            }
            else{
            }
        }

        if (getParam('notification.notify_ajax_refresh') > 0)
        {
            setTimeout('$.ajaxCall("notification.update", "", "GET");', (this.bDebug ? 10000 : (getParam('notification.notify_ajax_refresh') * 60000)));
        }
    }
};

只考虑上面代码中的以下两行,忽略其余代码:

$('.holder_notify_count').each(function(){
                iTotal += parseInt($(this).html());
            });

我无法理解 $(this) 包含什么?我在 $('.holder_notify_count').each(function(){.....});

中尝试了 alert($(this));

但它只打印了 [object Object],所以我无法找出 $(this) 包含的内容以及我应该如何打印它?

谢谢。

来自 Docs

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

根据您的代码

$('.holder_notify_count').each(function(){

});
回调方法中的

this引用底层DOM元素.holder_notify_count

$(this) 是对象 ($('.holder_notify_count')) $(this).html() 表示 $('.holder_notify_count')

里面的 html
$('.holder_notify_count').each(

将遍历具有 class .holder_notify_count

的每个元素

每个回调中的 $(this) 是一个 jQuery 对象,其中包含该迭代的特定 DOM 元素 .holder_notify_count

它包含在 jQuery 包装器中,可以访问所有 jQuery API 调用。

您在此 .each 循环中迭代的是元素集合中的每个 jQuery 对象。所以每个元素都有 class 命名为 holder_notify_count,如 jQuery 选择器 .holder_notify_count.

所示

这与您分配它相同 $('.holder_notify_count')[0],然后 $('.holder_notify_count')[1] 等等。

.each() 下警报不起作用。试试 console.log($(this).html());.

Satpal 确实给了你正确的答案,但这里的功能分解可以让你更好地理解:

         <ul>
            <li>Hello</li>
            <li>Hello-1</li>
            <li>Hello-2</li>
        </ul>

Jquery::

$(function () {
            $('ul li').each(function(){
                console.log($(this).text());
            })
        });

以上代码将打印以下内容:

"Hello" 
"Hello-1" 
"Hello-2"

为什么?

1st 请注意我们是如何选择 $('ul li') 的,我们正在选择 ul 中的所有 li,现在每个人都会在 1 号遍历所有 li 的 SO iteartion $(this) is the 1st li on the second $(this) is the secound li abd on the 3rd $(this) is the 3rd li