如何从".item"函数内部获取getJSON元素的ID?

jQuery(document).ready(function() {

    $(".item").tooltip({
        items: "div",
        content: function(callback) {

            $.getJSON('mydata.json', function(data) {

                var country = event.target.id;

                console.log(country);

            });

        }

    });

});


我已经看到了here的解释,但不确定如何传递事件。

最佳答案

content回调中,this指的是其工具提示被调用的元素,因此您可以使用this.id来获取当前元素的id

jQuery(function ($) {
    $(".item").tooltip({
        items: "div",
        content: function (callback) {
            var el = this;
            $.getJSON('mydata.json', function (data) {
                var country = el.id;
                console.log(country);
            });
        }
    });
});

关于javascript - jQuery从嵌套函数中获取触发事件的元素的ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28665614/

10-12 14:37
查看更多