jQuery Mobile初始页面DOM Cache所引发的问题
HTML元素事件多次触发:
- jsFiddle: http://jsfiddle.net/gn9JA/2/
- cause: 在jsFiddle示例中绑定了pageshow事件处理函数,因为btnTest按钮一直存在于DOM中,所以每次pageshow都会为btnTest按钮添加一次事件
- solution:
//off click event handler first
$('#btnTest').off('click').on('click', function(){
alert(1);
});
使用jQuery无法获得正确的元素(could not get the correct elements using jquery selectors):
- jsFiddle: http://jsfiddle.net/gn9JA/2/
- cause: btnTest按钮同时存在于page1与page2,因为先后顺序的关系,使用$('#btnTest')只可以获得page1中的按钮
- solution:
//add activePage as scope when jquerying elements
$('#btnTest',$.mobile.activePage).off('click').on('click', function(){
alert(1);
});
jQuery Mobile里初始页面的DOM Cache怎么删除?
相关链接:
为什么要移除第一页的DOM缓存:
- eg: visit the 1st page, and then navigate to 2nd page, change some user preference,
- back to the 1st page, you will find nothing happened because of DOM cache of the 1st page.