javascript - 使用 jQuery Mobile 时无法返回元素的 width()

标签 javascript jquery html css jquery-mobile

测试演示页面:http://jsfiddle.net/rCKzs/

<script type="text/javascript">
    $(document).ready(function() {  
        alert( $("#test").width() );          
    });
</script>

..

<div id="test">content</div>

如果我删除了 jQuery Mobile,那么它没问题,否则返回 0。

最佳答案

不要将 document.ready 与 jQM 一起使用:

Important: Use pageInit(), not $(document).ready()

The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event. This event is explained in detail at the bottom of this page.

另外jQM只支持jQuery 1.6.4

我会像这样使用 live()

$('div').live('pageshow',function(event, ui) {
    alert($("#test").width());  
});

关于javascript - 使用 jQuery Mobile 时无法返回元素的 width(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8770653/

相关文章:

javascript - 为什么 jQuery 示例经常省略脚本类型?

javascript - ajax 调用返回值和我的 html 页面

javascript - 替换 json/jsObject/string 中的多个值

javascript - 将 asp net linkbutton onclick 和 onclientclick 与 javascript 弹出窗口结合起来

javascript - 在 Ember-data 中以自定义方式保存属性

javascript - 如何在 html 页面中放置日期时间选择器?

Javascript 迭代器不起作用

php - 使用 Ajax 在过滤器中显示结果数的最佳方法

html/css 不能用百分比改变图像高度

javascript - 如何确定绝对定位元素的容器