jQuery 当前元素的警报 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9053983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:16:02  来源:igfitidea点击:

Alert ID of current element

jquery

提问by dynid

I'm using following code to alert id of current element.

我正在使用以下代码来提醒当前元素的 id。

<input type="text" id="desc" name="txtTitle" onclick="fun()">

jquery:

查询:

function fun () {
    var currentId = $(this).attr('id');
    alert(currentId);
}

Why does it alert "undefined"? I have tried with:

为什么会提示“未定义”?我试过:

var currentId =$('element').attr('id'); 
// and 
alert($(this).id); 
// and 
alert(this.id);

but it alerts undefined

但它提醒 undefined

采纳答案by Pointy

Try changing it to:

尝试将其更改为:

<input type="text" id="desc" name="txtTitle" onclick="fun.call(this)">

Better, bind your event handler with jQuery, since you're using it anyway:

更好的是,使用 jQuery 绑定您的事件处理程序,因为您无论如何都在使用它:

$(function() { $('#desc').click(fun); });

The reason your code doesn't work is that you're calling fun()from inside the event handler function constructed by the browser for your "onclick" attribute. By just calling the function like that, you provide no "receiver" object — nothing for thisto be, that is. If you call it with .call()however you can explicitly do that.

您的代码不起作用的原因是您fun()从浏览器为“onclick”属性构造的事件处理函数内部调用。只需像这样调用函数,您就不会提供“接收器”对象——没有什么this是可以的。如果你用.call()然而调用它,你可以明确地做到这一点。

回答by Bojangles

$(this)only works inside jQuery functions; it references nothing inside fun(). Instead, try this:

$(this)仅适用于 jQuery 函数;它里面没有引用任何东西fun()。相反,试试这个:

$('input#desc').click(function() {
    alert($(this).attr('id'));
});

With this HTML:

使用此 HTML:

<input type="text" id="desc" name="txtTitle">

It's not particularly good practice to have onClick=""attributes in your HTML, hence the $.click()function given above. You should always put your JavaScript in a separate file (especially when using jQuery).

onClick=""在 HTML 中使用属性并不是特别好的做法,因此是$.click()上面给出的函数。您应该始终将 JavaScript 放在一个单独的文件中(尤其是在使用 jQuery 时)。

回答by Elyor

just use

只是使用

alert($(this).attr('id'));

回答by Lanil Marasinghe

You can just use alert(event.target.id);inside the function which is called by your element without passing any argument to the function.

您可以只alert(event.target.id);在元素调用的函数内部使用,而无需向函数传递任何参数。

回答by Pekka

If you pass a string to the onclickevent, the code in that string will be the function that is executed. If it calls another function, thiswill not be defined.

如果将字符串传递给onclick事件,则该字符串中的代码将是执行的函数。如果它调用另一个函数,this则不会被定义。

Either do

要么做

$("#desc").click(fun)

on document ready, or if you must work with inline events, pass thisas an argumentuse the method shown by Pointy.

在文档准备好时,或者如果您必须使用内联事件,请使用 Pointy 显示的方法作为参数传递this

回答by SmokeyPHP

Change the element to say:

将元素更改为:

onclick="fun(this)"

And the function to:

和功能:

function fun(elem)
{
    var currentId = $(elem).attr("id");
}

回答by xdazz

That is because you called thisin global scope.

那是因为您this在全局范围内调用。

Without jQuery, you can do like below:

如果没有 jQuery,您可以执行以下操作:

<input type="text" id="desc" name="txtTitle" onclick="fun(this)">

function fun(el) {
    alert(el.id);
}

With jQuery, you can do like below:

使用 jQuery,您可以执行以下操作:

<input type="text" id="desc" name="txtTitle">

$(function(){
    $('input[type="text"]').click(function(){
        alert(this.id);
    });
});