2016-07-12 8 views
0

Ich habe mehrere div in einem Webformular und wenn ich auf ein bestimmtes div geklickt habe, muss ich einige Aufgabe auf der Grundlage der div-ID durchführen.Wie finde ich die div-ID auf Mausklick in jquery?

Gibt es eine Möglichkeit, die div-ID per Mausklick zu finden?

<table> 
 
    <tr> 
 
    <td> 
 
     <div id="firstdiv">div1</div> 
 
    </td> 
 
    <td> 
 
     <div id="seconddiv">div2</div> 
 
    <td> 
 
    <td> 
 
     <div id="thriddiv">div3</div> 
 
    </td> 
 
    <td> 
 
     <div id="fourthdiv">div4</div> 
 
    </td> 
 
    </tr> 
 
</table>

+1

Verwendung '$ (this) .attr ('id')' Innenrasterung Event wird es die ID von div bekommen – guradio

+0

überprüfen Sie diese [Demo] (https://jsfiddle.net/6jLm8kvs/) – guradio

+0

Ich denke, Sie sollten Ihre Tags richtig schließen – Coderchu

Antwort

3

Bitte versuchen Sie dies:

$("table div").click(function() { 
    var divID = this.id;//Or $(this).attr("id"); 
    //Your code here 
}); 
1
  • Bind click Ereignis über div Element
  • this verweist Element geklickt click-handler
  • Zugang id Eigenschaft der element

$('table div').on('click', function() { 
 
    console.log(this.id); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td> 
 
     <div id="firstdiv">div1</div> 
 
    </td> 
 
    <td> 
 
     <div id="seconddiv">div2</div> 
 
    </td> 
 
    <td> 
 
     <div id="thriddiv">div3</div> 
 
    </td> 
 
    <td> 
 
     <div id="fourthdiv">div4</div> 
 
    </td> 
 
    </tr> 
 
</table>

1

$('div').click(function() { 
 
    console.log($(this).attr('id')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td> 
 
     <div id="firstdiv">div1</div> 
 

 

 
     <td> 
 
     <td> 
 
      <div id="seconddiv">div2</div> 
 
      <td> 
 
      <td> 
 
       <div id="thriddiv">div3</div> 
 
       <td> 
 
       <td> 
 
        <div id="fourthdiv">div4</div> 
 
        <td> 
 
        <tr> 
 
         <table>

0

Wenn Sie eine Funktion zum Zeitpunkt auszuführen auf div klicken, von dem anruf ID, das ist der Code:

<head> 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

$(document).ready(function() { 

    $('#firstdiv').click(function(){ 

    /*I add alert for the example, but here is where you put your code*/ 
    alert($(this).attr('id')); 

}); 

</head> 

<body> 

<div id="firstdiv">div1</div> 

</body> 

Neben diesem die Informationsverbindung mit der Funktion Klick() in jquery, hat Beispiel dafür, wie es in verschiedenem Ereignisse auf einem Element zu verwenden: https://api.jquery.com/click/

Beispiel die div innen Tabelle Aufruf:

$("table div").click(function(){ 

    /*I add alert for the example, but here is where you put your code*/ 
    alert($(this).attr('id')); 

}); 

Beispiel mit dem Tag div Aufruf:

$('div').click(function() { 

    /*I add alert for the example, but here is where you put your code*/ 
    alert($(this).attr('id')); 

}); 

--body

<table> 

<tr> 

<td><div id="demo1"></td> 

</tr> 

</table> 

Und das ist ein Beitrag mit gutem Beispiel: How can I get the ID of an element using jQuery?

0

Hier ist eine einfache, aber effektive Antwort

<div id="someId" onclick="somefunction(this.id)"> 
    Click me 
</div> 
<script> 
    function somefunction(id){ 
    alert(id); 
    } 
</script> 
Verwandte Themen