【问题标题】:jQuery .find not working in SafarijQuery .find 在 Safari 中不起作用
【发布时间】:2014-02-18 10:21:35
【问题描述】:

这在 Firefox 和 Chrome 中运行良好。但为什么不在 Safari 中呢?

 $('.bxslider li .image').each(function() {
   $(this).width($(this).find('img').width());
    });

Safari 中的结果是style="width: 0px; "。 也试过 $(document).ready,没有变化。

感谢您的帮助!

【问题讨论】:

  • 图片可能还没有加载
  • 在 $(window).load() 中试试这个代码

标签: jquery safari


【解决方案1】:

正如已经建议的那样,在加载所有图像后使用window.load() 处理程序运行代码。

或者使用

jQuery(function ($) {
    $('.bxslider li .image img').load(function () {
        $(this).closest('.image').width(this.width)
    }).filter(function () {
        return this.complete;
    }).trigger('load')
})

【讨论】:

    【解决方案2】:

    尝试将您的代码放入$(window).load(function() { }) 以确保在更改宽度之前正确加载所有图像。

    $(window).load(function() {
        $('.bxslider li .image').each(function() {
            $(this).width($(this).find('img').width());
        });
    });
    

    【讨论】:

    • 我之前试过这个,但是没有用。无论如何感谢您的帮助!
    【解决方案3】:

    您也可以使用 ">" 运算符来代替 find。 例如:

    $('.bxslider li .image').each(function() {
       $(this).width($(this+'> img').width());
        });
    

    您也可以在 CSS 中使用此运算符: 例如:

    body > h1
    

    匹配 body-Tag 中的所有 h1-Tag。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-06
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多