简体   繁体   English

将js代码转换为jQueryMobile时出错

[英]error in converting js code to jQueryMobile

I have a javascript/jquery code http://jsfiddle.net/9773D/ for timer. 我有一个javascript / jquery代码http://jsfiddle.net/9773D/为计时器。

I am trying to port it to jQuery Mobile code but I am confused about the pageinit,bind,live etc window events. 我试图将它移植到jQuery Mobile代码,但我对pageinit,bind,live等窗口事件感到困惑。

In the code i see that the error is because some elements in the tick() function are not loaded in DOM before it has been invoked in the code. 在代码中我看到错误是因为tick()函数中的某些元素在代码中调用之前未加载到DOM中。 Can someone help me in correcting my code. 有人可以帮我纠正我的代码。

Thanks, 谢谢,

I changed your line to 我把你的线改成了

var timeDisplay = $(".time")[0];

and that fixes it. 并修复它。

Edit: 编辑:

Adding explanation: 添加说明:

Since you used innerHTML instead of $('.time').html("") , you needed to set timeDisplay to the HTML node, since innerHTML is a property of the node, not the jQuery object that is returned by the selector $('.time') . 由于你使用innerHTML而不是$('.time').html("") ,你需要将timeDisplay设置为HTML节点,因为innerHTML是节点的属性,而不是选择器$('.time')返回的jQuery对象。 $('.time')

Here's a jsfiddle that shows how it could be done in a more jquery'sh way. 这是一个jsfiddle,展示了如何以更加jquery的方式完成它。 http://jsfiddle.net/9773D/1/ http://jsfiddle.net/9773D/1/

timedisplay.innerHtml = "" //does not work since timedisplay is a jquery object 
//timedisplay[0] is the html object so on that innerHtml does work

But jquery has the function .html(""); 但是jquery有函数.html("");

timedisplay.html(""); // is a bit cleaner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM