2

如果使用此代码,我的代码将起作用:

for(i=0; i<listUsers.length; i++){
   if(listUsers[i] != usr){
      $( "#listHisUser" ).append('<li><a href="#hist_cont" onclick="getChatContent()">'+listUsers[i]+'</a></li>').listview('refresh');
   }
}

function getChatContent(){
   alert("data");
}

但如果我把它改成这样:

for(i=0; i<listUsers.length; i++){
   if(listUsers[i] != usr){
      $( "#listHisUser" ).append('<li><a href="#hist_cont" onclick="getChatContent('+listUsers[i]+')">'+listUsers[i]+'</a></li>').listview('refresh');
      }
}

function getChatContent(data){
      alert(data);
      alert("data");
}

它出现错误:我单击的列表视图项目ReferenceError: Can't find variable: tqminh at file:///android_asset/www/index.html#hist:1在哪里。tqminh

<!-- History -->
<div data-role="page" id="hist" data-title="HISTORY">
   <div id="a" data-role="header" data-theme="b">
   <h1 style="text-align: left;font-size: 20px">History Chat</h1>
   </div>       
   <div data-role="content" data-inset="true">
   <ul data-role="listview" data-theme="c">
   <li><a href="#" onclick="getChatContent('tqminh')">tqminh</a></li>
   </ul>
   </div>
</div>
4

1 回答 1

0

您的函数参数未呈现为字符串。尝试更换

<a href="#hist_cont" onclick="getChatContent('+listUsers[i]+')">'+listUsers[i]+'</a>

<a href="#hist_cont" onclick="getChatContent(\''+listUsers[i]+'\')">'+listUsers[i]+'</a>
于 2012-04-18T14:48:43.033 回答