jQuery 追加看不到变化?
我有一些修改我的网络应用程序的脚本。 它的基本作用是:
- 将动态元素添加到 DOM
- 调整一些 DOM 元素的大小以适应窗口高度和其他内容...
所以,我这样做:
$(document).ready ->
$('.interval-view').wrapInner '<div class="column" />'
$('.column').wrapInner '<div class="inner-column" />'
$('#contents, #footers').append '<div class="clearfix"></div>'
$('.interval-view:even').css 'background-color', '#F9F9F9'
$('.interval-view:odd').css 'background-color', '#DDD'
resize = ->
$('.column').height $(window).height() - $('#filter-list').outerHeight(true) - $('#footers').outerHeight(true)
$('#timeline-panel').width $(window).width()
$('#timeline-panel .scrollable-area').width "#{$('.interval-view').length * $('.interval-view').width()}px"
window.onorientationchange = ->
resize()
$(window).resize ->
resize()
resize()
但是,我没有在 $(' 上获得正确的高度值.column').高度
。该脚本向我检索高度值,就好像我没有附加 clearfix
元素一样。就好像它在计算最终高度时没有考虑到该部分。
我什至尝试过使用延迟对象,但没有成功。它仍然需要高度,而不考虑 clearfix
元素。
对于不是 CoffeeScript 的人,我在这里粘贴生成的 Javascript:
$(document).ready(function() {
var resize;
$('.interval-view').wrapInner('<div class="column" />');
$('.column').wrapInner('<div class="inner-column" />');
$('#contents, #footers').append('<div class="clearfix"></div>');
$('.interval-view:even').css('background-color', '#F9F9F9');
$('.interval-view:odd').css('background-color', '#DDD');
resize = function() {
$('.column').height($(window).height() - $('#filter-list').outerHeight(true) - $('#footers').outerHeight(true));
$('#timeline-panel').width($(window).width());
return $('#timeline-panel .scrollable-area').width("" + ($('.interval-view').length * $('.interval-view').width()) + "px");
};
window.onorientationchange = function() {
return resize();
};
$(window).resize(function() {
return resize();
});
return resize();
});
有什么方法可以解决这个问题吗?
I have some scripting that modifies my web application.
What it basically does is:
- Add dynamic elements to the DOM
- Resize some DOM elements to fit the window height and other stuff...
So, I do:
$(document).ready ->
$('.interval-view').wrapInner '<div class="column" />'
$('.column').wrapInner '<div class="inner-column" />'
$('#contents, #footers').append '<div class="clearfix"></div>'
$('.interval-view:even').css 'background-color', '#F9F9F9'
$('.interval-view:odd').css 'background-color', '#DDD'
resize = ->
$('.column').height $(window).height() - $('#filter-list').outerHeight(true) - $('#footers').outerHeight(true)
$('#timeline-panel').width $(window).width()
$('#timeline-panel .scrollable-area').width "#{$('.interval-view').length * $('.interval-view').width()}px"
window.onorientationchange = ->
resize()
$(window).resize ->
resize()
resize()
But, I don't get the proper height value on $('.column').height
. The script retrieves to me the height value as if I didn't append the clearfix
element. It's like it doesn't take into account that part when it calculates the final height.
I've even tried with deferred object, but no success. It still takes the height without the clearfix
element consideration.
For not CoffeeScript folks, here I paste the generated Javascript:
$(document).ready(function() {
var resize;
$('.interval-view').wrapInner('<div class="column" />');
$('.column').wrapInner('<div class="inner-column" />');
$('#contents, #footers').append('<div class="clearfix"></div>');
$('.interval-view:even').css('background-color', '#F9F9F9');
$('.interval-view:odd').css('background-color', '#DDD');
resize = function() {
$('.column').height($(window).height() - $('#filter-list').outerHeight(true) - $('#footers').outerHeight(true));
$('#timeline-panel').width($(window).width());
return $('#timeline-panel .scrollable-area').width("" + ($('.interval-view').length * $('.interval-view').width()) + "px");
};
window.onorientationchange = function() {
return resize();
};
$(window).resize(function() {
return resize();
});
return resize();
});
Any way to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可以通过使用 jquery .live 来解决
this might be resolved by using jquery .live
.live() 用于将事件绑定到页面加载后动态创建的元素。
您使用什么浏览器? jQuery 的版本是什么?因为我记得 .heigth() 和某些浏览器有一个错误。
.live() is used for bind an event to alement created dynamically after the page is loaded.
What browser are you using? and what version of jQuery? Because I remeber that a there was a bug with .heigth() and certain browser.
请注意,您可以将多个事件参数传递给 jQuery live,如下所示:
Note that You can pass more than one event argument to jQuery live like this: