尝试获取.each()中元素的宽度

时间:2018-10-18 12:30:45

标签: javascript jquery each offsetwidth

我正在尝试在.each()函数内部获取元素的宽度,但是我却遇到了无法正常工作的问题。

jQuery(document).ready(function($) {
    $('.tooltip').each(function() {
        var self = $(this);

        var width = self.find("#testing").offsetWidth;
        alert(width); //Returns undefined

        $(this).find(".tooltip-icon").hover(function(){
            self.find(".tooltip-text").show();
        }, function () {
            self.find(".tooltip-text").hide();
        });
    });
});

1 个答案:

答案 0 :(得分:1)

您在同一行中使用jQuery和javascript

执行此操作

var width = self.find("#testing").width(); // jquery. you should change the name of the `self` variable to another one
相关问题