【发布时间】:2014-04-03 13:18:28
【问题描述】:
您好,我有两个函数,一个带有一个参数,另一个称为“goToMatchDetailPage”,带有两个参数。我试图将这两个变量传递给第二个函数,但它最终只传递给第一个参数并将第二个参数留空,并显示“未定义”。
match.Home 和 match.Away 是我想通过“goToMatchDetailPage”传递的两个动态变量。 它将 match.Home 和 match.Away 作为一个变量,我希望它们分开。
这里是代码
function generateMatchLink(match){
//<li data-role="list-divider">Divider</li>
var currentDate = match.Date;
var time = match.Time.replace(":00", "");
if(oldDate != currentDate){
//debugger
oldDate = match.Date;
return '<li data-role="list-divider" data-theme="b">\ ' + oldDate + '\ </li><li data-icon="false"><a href="javascript:void(0)'
+ '" onclick="goToMatchDetailPage(\''
+ match.Home + ',' + match.Away +'\')">'
+ ' <div class="ui-grid-a" data-theme="none" style="height:20px">'
+ ' <div class="ui-block-a" style="width:40%">' + match.Home + '</div>'
+ ' <div class="ui-block-b" style="width:20%">' + time + '</div>'
+ ' <div class="ui-block-c" style="width:40%">' + match.Away + '</div></div></a></li>';
}else{
return '<li data-icon="false"><a href="javascript:void(0)'
+ '" onclick="goToMatchDetailPage(\''
+ match.Home + ',' + match.Away +'\')">'
+ ' <div class="ui-grid-a" data-theme="none">'
+ ' <div class="ui-block-a" style="width:40%">' + match.Home + '</div>'
+ ' <div class="ui-block-b" style="width:20%">' + time + '</div>'
+ ' <div class="ui-block-c" style="width:40%">' + match.Away + '</div></div></a></li>';
}
}
这是第二个功能
function goToMatchDetailPage(matchHome, matchAway){
//create the html template
var matchPage = $("<div data-role='page' data-url=dummyUrl><div data-role='header'><h1>"
+ matchHome + "</h1></div><div data-role='content'><div data-role='tabs'>"
+ "<div data-role='navbar'>"
+ "<ul>"
+ "<li><a href='#fragment-1'>" + matchHome + "</a></li>"
+ "<li><a href='#fragment-2'>" + matchAway + "</a></li>"
+ "</ul>"
+ "</div>"
+ "<div id='fragment-1'>"
+ "<p>This is the content of the tab 'One', with the id fragment-1.</p>"
+ "</div>"
+ "<div id='fragment-2'>"
+ "<p>This is the content of the tab 'Two', with the id fragment-2.</p>"
+ "</div></div></div>");
//append the new page to the page contanier
matchPage.appendTo($.mobile.pageContainer);
//go to the newly created page
$.mobile.changePage(matchPage);
}
【问题讨论】:
-
只是一个风格说明:看在上帝的份上,看看使用虚拟脚本标签来保存你的 HTML 模板。把它全部放在串在一起的代码中是一场等待发生的维护灾难:)
-
我会看看虚拟脚本,但你有答案吗?
-
在使用之前检查
matchAway是否已设置,例如if(matchAway) -
我发布了更多代码
-
更新我的第一条评论:把它全部放在串在一起的代码中是维护灾难确实发生了:)
标签: javascript jquery jquery-mobile