2012-03-12 20 views
0

jqueryのコンテキストメニューをページに使用していますが、このdivの右ボタンをクリックすると特定のdivのIDを取得できません。Div Idを右クリックする方法

+0

「divを右クリックする」または「div内のボタンをクリックする」という意味ですか? – zzzzBov

答えて

0
$("#id").click(function() { console.log($(this).attr("id")); }) 

jQueryでは、イベントが発生したオブジェクトは常にこれです。ここ

+2

私はOPが右クリックを求めていると思う。 – m90

+0

私が理解したように、IDを取得する方法が問題でした。 – ckruse

6
$('#element').on("contextmenu",function(){ 
    alert(this.id); 
}); 

又は

$('#element').on('mousedown', function(e) { 
    if (e.which === 3) { 
     alert(e.target.id);   
    } 
}); 
0

は一例です。

HTML ::

<div id="buttondiv"> 
    <button type="button">Click Me!</button> 
</div>​ 

jqueryのコード:

$(document).ready(function(){ 
    $("button").on("mousedown", function(e){ 
     if(e.which == 3){ 
      var divId = $(this).parent().attr("id"); 
      console.log(divId); 
     } 
    }) 
});​ 
0

は、私はあなたが

これは、任意の<div>上で動作する "divの上で右クリック" を意味すると仮定していますページ:

//bind the event to the document, using the delegate of "div" 
$(document).on('mousedown', 'div', function (e) { 
    //check that the right mouse button was used 
    if (e.which === 3) { 
     //log the [id] attribute of the element that was right-clicked on 
     console.log($(this).attr('id')); 
    } 
}); 
関連する問題