OnClick只能在重新加载后使用jquerymobile工作

我知道这是一个经常被问到的问题,但没有答案(到现在为止)适合我。

我和Cordova一起写了一个移动应用程序 我也在浏览器(Firefox)中测试应用程序。 我正在使用jQuery和jq mobile。

问题是:我的OnClick事件仅在刷新后工作,这在移动设备上是不可能的,甚至在PC上也不是真正的解决方案。

更新:我读到,在jQuery mobile中没有再次加载ehader。 所以我按照thsi解决方案中的描述对其进行了调整: 使用jQuery-mobile过渡加载不同的页面

没用。

并使用alert(); 你看,脚本运行一次但在网站完全构建之前。

我的HTML:

  

最初有一个onclick="doStuff()"

这是我的不同尝试:

 $(function () { // Assign the click handler on DOM-ready $('a').on('click', function () { dateElementClicked(this); }); }); $(document).ready($(function () { // Assign the click handler on DOM-ready $('a').on('click', function () { dateElementClicked(this); }); }) ); $("#anchor0").live("click", dateElementClicked($("#anchor0"))); $("a").click( dateElementClicked(this)); $("a").bind("click", function (event, ui){ dateElementClicked(this); }); 

它们都只有在刷新后才能工作。 或者第一个运行函数即时并中断所有内容因为“this”未定义。

编辑

我甚至用按钮和inpute类型按钮尝试了它,并制作了额外的js文件。 但是我的javascript只在刷新后运行…在脚本中输入了一个控制台日志和警报。 所以整个脚本都被卡住了

dateelement单击的函数(我也清除了它以进行测试,只是在其中放入一个alert()

以下是该项目的git链接: https : //github.com/LosKartoflos/Apoll.git

 function dateElementClicked(clickedAnchor) { //anchor is clicked the first time(the id number equals numberOfAppointments) if (clickedAnchor.id.slice(-1) == numberOfAppointments) { dateElementClickedFirstTime(clickedAnchor); } else if (appointmentList[getDateElementNumber(clickedAnchor)]["RolledOut"] == true) { hideContent(getDateElementNumber(clickedAnchor)); } else if (appointmentList[getDateElementNumber(clickedAnchor)]["RolledOut"] == false) { showContent(getDateElementNumber(clickedAnchor)); } else { alert("Element not listed"); } } 

BTW:我的脚本在我的html文件中。

试试这个

 $(document).on('click', '#anchor0', function(event) { }); 

或这个

 $(document).on('click', 'a', function(event) { }); 

好吧问题是,Cordova正在搞乱正常的构建/加载oder。 在侧面加载后触发function。

Cordova纪录片推荐这两个解决方案:

把它放在你的标题中,并在onload或dofirst中绑定你的事件。 在页面准备就绪后,执行您想要完成的所有操作:

    

或者将它放在onDeviceReady函数中,在auto创建的index.js中。

  // deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicitly call 'app.receivedEvent(...);' onDeviceReady: function() { document.getElementById("anchor0").addEventListener("click", clicked, false); app.receivedEvent('deviceready'); }, 

这部纪录片: https : //cordova.apache.org/docs/en/4.0.0/cordova_events_events.md.html

我踢出了jquery和jquery mobile。 Jquery正在弄乱文档就绪,jquery移动可以防止头部再次加载。