javascript 获取被点击的元素的 ID 并作为参数传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17173424/
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-10-27 07:22:18  来源:igfitidea点击:

Get ID of Element Being Clicked and Pass as Parameter?

javascriptjqueryfunction

提问by Aaron Brewer

How does one, through jQuery, get the ID of an element that is being clicked on and then pass it as a parameter into a function? Example jQuery code below.

如何通过 jQuery 获取被点击的元素的 ID,然后将其作为参数传递给函数?下面的示例 jQuery 代码。

jQuery(document).ready(function() {
    var id = this_id;
    jQuery(".lightbox a").click({param: id}, functionName);
});

May I note that the "param" parameter is integral to the structure of the function.

我可以注意到“param”参数是函数结构的组成部分。

Apologies all, I am no Javascript master by any means.

抱歉,无论如何,我都不是 Javascript 高手。

回答by MrCode

Within the click handler, you can access the element ID with this.idor $(this).attr('id'):

在单击处理程序中,您可以使用this.id或访问元素 ID $(this).attr('id')

jQuery(document).ready(function() {
    jQuery(".lightbox a").click(function(){
        functionName(this.id);
    });
});

回答by adeneo

I'm guessing the point is to pass event data to a function that expects that, as ,click()supports the .click( [eventData ], handler(eventObject) )syntax, and if so, you have to iterate the collection yourself:

我猜重点是将事件数据传递给一个期望 asclick()支持.click( [eventData ], handler(eventObject) )语法的函数,如果是这样,你必须自己迭代集合:

jQuery(document).ready(function($) {
    $(".lightbox a").each(function() {
        $(this).click({param: this.id}, functionName);
    });
});

EDIT:

编辑:

You could do this with on()as well:

你也可以这样做on()

jQuery(document).ready(function($) {
    $(".lightbox a").each(function() {
        $(this).on('click', {param: this.id}, functionName);
    });
});

FIDDLE

小提琴

回答by tymeJV

You can use this.idinside a click event, example:

您可以this.id在点击事件中使用,例如:

jQuery(".lightbox a").click(function() {
    var id = this.id;

    //pass to a function
    testFunction(id);
});

function testFunction(param) {
    console.log(param);
}

回答by Skatox

It's easy just access to the thiselement to get the clicked element, then extract its idand save it into a variable like this:

只需访问this元素即可轻松获取单击的元素,然后提取其id并将其保存到如下变量中:

jQuery(".lightbox a").click(function(){
  var id = jQuery(this).attr("id");
  callFunction(id);
});

回答by A. Wolff

http://jsfiddle.net/pArW6/

http://jsfiddle.net/pArW6/

jQuery(document).ready(function() {
   jQuery(".lightbox a").click(functionName);
});

function functionName()
{ 
  alert(this.id);   
}

回答by pala?н

You can do this:

你可以这样做:

jQuery(document).ready(function () {
    jQuery(".lightbox a").click(function (e) {

        // Cancel the default action (navigation) of the click.
        e.preventDefault();

        // 'this' here refers to the link being clicked in the current scope
        // you can check the console for the id for debug purpose
        console.log(this.id);

        // pass the id to the function
        functionName(this.id);
    });
});

回答by asafreedman

Another way is to use the event parameter that gets passed to the callback function.

另一种方法是使用传递给回调函数的事件参数。

jQuery(".lightbox a").click(function(ev) {
    console.log(ev.target.id);
}

Of course it's a mix of jQuery and pure JS.

当然,它是 jQuery 和纯 JS 的混合体。

回答by Thomas Junk

Usually you have a function for an event declared with function(event) and the event has a target and the id of the target is, what you want. So

通常你有一个用 function(event) 声明的事件的函数,并且该事件有一个目标,目标的 id 是你想要的。所以

$("SomeElement").on("click", function(e){ callanotherFunction(e.target.id) })

does, what you wanted

做,你想要的

回答by Grant Thomas

You can use this.idor $(this).attr("id");, but you might want to get a reference to $(this)- wrapped or not - immediately and work from a variable if you do much of anything else in there.

您可以使用this.idor $(this).attr("id");,但您可能希望立即获得对$(this)- 包装或不包装 -的引用,如果您在其中做了很多其他事情,则可以从变量开始工作。

回答by Misters

You can you Use $(this).att("id").

你可以使用$(this).att("id").

$(".lightbox a").click(function() {
    var ID=$(this).att("id");

    //pass to a function
    TestFunction(ID);
});

function TestFunction(P) {
    console.log(P);
}

Live example

活生生的例子

http://jsbin.com/enobop/1/edit

http://jsbin.com/enobop/1/edit