1

我在下面有这段代码,它运行良好,但似乎没有必要,因为它循环遍历所有 div.test,即使我从未将鼠标悬停在它们上面。我尝试将 .qTip() 函数放在 mouseover 事件中以提高效率,但这似乎不起作用。

有关如何使以下代码更高效的任何建议?

<script type="text/javascript">
    $(document).ready(function () {

        $('div.test').each(function () {

            var tooltipHtml = $(this).next('.tooltip').html();
            $(this).qtip({
                content: {
                    text: tooltipHtml
                },
                style: { width: 450 }
            });
        });
    });
4

1 回答 1

1

你可以像这样改进它:

$(function () {
    $('div.test').each(function () {
        $(this).qtip({
            content: $(this).next('.tooltip'),
            style: { width: 450 }
        });
    });
});

content选项采用 jQuery 对象(在文档中称为 jQuery DOM 数组),因此无需为每个对象抓取 HTML。 但是,如果您仍然绑定大量这些(数百个或更多)性能,则可能仍然不是您在旧浏览器中所追求的。

于 2010-08-01T12:03:40.987 回答