被动态按钮文本更新困扰

时间:2013-11-16 22:56:32

标签: jquery button mobile dynamic text

我一直试图弄清楚这一天,并且可以解决这个问题。我的代码在jsfiddle上完美运行,但在我的本地环境中却没有。

我确保jQuery和CSS的版本是相同的。

我的代码:

<!DOCTYPE html>
<html>
<head>
<title>title</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile.structure-1.3.2.css" />
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery.mobile-1.3.2.js"></script>
<script src="js/date.js"></script>
</head>

<body>

<div data-role="page" id="p_home" data-theme="b">

    <div data-role="controlgroup">
 <a href="" data-role="button" data-icon="arrow-l" id="prev">prev</a>
 <a href="" data-role="button"  id="today"></a>
 <a href="" data-role="button" data-icon="arrow-r" id="next">next</a>
   </div>

  </div>
 <script>
// show date
showDate(0);

// set up prev day
$("#prev").click(function (e) {
showDate(-1);
 });

// set up next day
$("#next").click(function (e) {
showDate(+1);
 });

function showDate(ddiff) 
{
var tday;
if (ddiff === 0)
{
   tday = Date.today().toString('MMMM d, yyyy');
}
else 
{
   var cur=$("#today").text();
   tday = Date.parseExact(cur, 'MMMM d, yyyy').addDays(ddiff).toString('MMMM d, yyyy');
}
    $("#today .ui-btn-text").text(tday);
}
</script>
</body>
</html>

这是fiddle

我的问题是,在我的本地环境中,当我执行$(“#today .ui-btn-text”)。text(tday)时,showDate(0)不起作用但是如果我使用$(“#今天“)。text(tday)它更新日期就好了。

为什么会出现这种情况?

2 个答案:

答案 0 :(得分:0)

这可能是jQuery如何选择的问题。

试试这个

$(".ui-btn-text", "#today").text(tday);

更新的小提琴在这里http://jsfiddle.net/Ee9nq/33/

答案 1 :(得分:0)

您需要将脚本代码添加到

$(document).ready(function(){
...
});

它应该解决它。