什么是'$(这个)'包含在下面的javascript函数代码中

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

本文关键字:在下面 javascript 代码 包含 函数 这个 什么      更新时间:2023-09-26

以下是函数的代码:

$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)包含什么,我应该如何打印?

谢谢。

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

来自文档

.each()方法旨在使DOM循环结构简洁且不易出错。当被调用时,它会遍历作为jQuery对象一部分的DOM元素。每次运行回调时,都会从0开始传递当前循环迭代。更重要的是,回调是在当前DOM元素的上下文中触发的,因此关键字this指的是该元素。

根据您的代码

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

回调方法中的this指的是底层DOM元素.holder_notify_count

$('.holder_notify_count').each(

将迭代具有类别.holder_notify_count 的每个元素

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

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

在这个.each循环中迭代的是元素集合中的每个jQuery对象。因此,每个元素都具有名为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"

为什么?

首先注意我们是如何选择$('ul li')的,我们选择ul中的所有li,现在每个都将迭代所有li的SO,在第一个iteartion$(this(是第二个上的第一个li(this(在第三个上的第二个li abd$(this(是第三个li