简体   繁体   English

如何知道在jquery中点击了哪个id?

[英]How to know which id has been clicked in jquery?

I want to know that which button has been clicked in jquery.我想知道在 jquery 中点击了哪个按钮。 I have the html file:我有 html 文件:

<div class="row">
    <div class="col-md-6">
        <div class="well " id="option_a">
        </div>
    </div>
    <div class="col-md-6">
        <div class="well " id="option_b">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <div class="well " id="option_c">
        </div>
    </div>
    <div class="col-md-6">
        <div class="well " id="option_d">
        </div>
    </div>
</div>

I need to know which option has been clicked?我需要知道点击了哪个选项?

Assuming you are binding event with element with class " well "假设您将事件与类“ well ”的元素绑定

You can add code like following您可以添加如下代码

$(".well").click(function(){
     var id = $(this).attr("id");
});

Here is my suggestion:这是我的建议:

$('div.well').click(function() {
    console.log(this.id);
});

And here is the jsfiddle:这是jsfiddle:

http://jsfiddle.net/k2Lhekpp/ http://jsfiddle.net/k2Lhekpp/

There is alert instead of console.log and the div containers have red background color with width and height each 100px for better visualization of the clicks.alert而不是console.log并且div容器具有红色背景颜色,宽度和高度各为 100 像素,以便更好地可视化点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM