$(this).css(“width”) not working on ancohor tags in Safari

徘徊边缘 提交于 2019-12-11 04:38:34

问题


I'm using CSS selectors to generate custom pop-ups for a web templates - basically, some some the producers aren't comfortable with JS and I need an easy way for them to set pop-up sizes etc. I'm not really interested in the merits of this approach, but rather wish to understand why jquery seems to be unable to get the width and height as defined in the CSS for a anchor in Safari.

See: http://f1shw1ck.com/jquery_sandbox/csspops.html

The meat of my question:

For the selector a.popup {width: 800px;height: 560px;} and the anchor <a href="http://www.metmuseum.org/toah/hd/apol/hd_apol.htm" class="popup">African Rock Art: Apollo 11 Cave Stone (c. 25,500 - 23,500 BCE)</a> why are

if ($(this).css("width")) {   
windowParams.width = $(this).css("width");
}
if ($(this).css("height")) {   
windowParams.height = $(this).css("height");
}

both properties returning as zero in Safari?

onclick pop-up alert in other browsers: toolbar=no,directories=no,location=no,resizable=yes,menubar=no,scrollbars=yes,status=no,width=800px,height=560px,top=20,left=240

alert in Safari: toolbar=no,directories=no,location=no,resizable=yes,menubar=no,scrollbars=yes,status=no,width=0px,height=0px,top=20,left=640

Thanks for helping me understand.


回答1:


Andy is correct. There is a solution offered here:

Get the real width and height of an image with JavaScript? (in Safari/Chrome)




回答2:


It doesn't work in Chrome either.

From this link and this other Stack Overflow answer, it looks like the reason is that Webkit browsers will report '0' or 'unknown' as the size until it is fully loaded, which may be too late.

As Micah notes, this is because Javascript and CSS are loaded in parallel, which means that the styling may not be fully applied by the time the scripts run.



来源:https://stackoverflow.com/questions/1374914/this-csswidth-not-working-on-ancohor-tags-in-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!