如何在JQuery中的另一个对象中获取对象

时间:2015-09-15 11:06:12

标签: jquery bootbox

如何获得我在bootbox.confirm()内点击的li?

<ul id="something">
  <li>Result 1</li>
  <li>Result 2</li>
  <li>Result 3</li>
</ul>

$('#something li').click(function(){
    bootbox.confirm("Are you sure?", function(result) {
    if(result == true){
        alert("You deleted" + /*here I want to get the item that was clicked*/);
        }
    }); 
});

2 个答案:

答案 0 :(得分:1)

使用this缓存上下文:

$('#something li').click(function(){
    var $this = this; // cache it here
    bootbox.confirm("Are you sure?", function(result) {
        if(result == true){
           alert("You deleted" + $this.textContent/*use it here now!!*/);
        }
    }); 
});

答案 1 :(得分:0)

您可以在此上下文中使用$( this )

$('#something li').click(function(){
    console.log($( this ));
});

请参阅:https://api.jquery.com/click/