jQuery"这个"问题

Die*_*oP. 0 jquery

我是jQuery的新手.我试图将值传递给函数.

<div class="the_service" id="ecommerce" onClick="javascript: gorightthere(ecommerce);"> 

function gorightthere(this){

    var gothere = $('h2[name="'+this+'"]');
    if (gothere.length){
$('html, body').animate({
                    scrollTop: gothere.offset().top
                     }, 2000);
        removeClass('selected');
        gothere.addClass('selected');
    }
};
Run Code Online (Sandbox Code Playgroud)

我想要做的是将值"电子商务"传递给该功能.我想使用"this"因为我有许多不同的值要传递,我想做一个函数,它将与onClick事件后我将传递的每个值一起使用.

有帮助吗?

谢谢

Jam*_*son 7

您的变量名称与保留关键字冲突this.使用另一个变量名称,您应该全部设置.通常,您不应该使用this变量名,因为它在几乎所有基于C的语言中都具有非常特定的含义:

function gorightthere(value) {      
    var gothere = $('h2[name="' + value + '"]'); 
    if (gothere.length){ 
        $('html, body').animate({ 
            scrollTop: gothere.offset().top  
            }, 2000); 
        removeClass('selected'); 
        gothere.addClass('selected'); 
    } 
};
Run Code Online (Sandbox Code Playgroud)