【问题标题】:jquery mobile doesnt apply styles after ajax loadajax加载后jquery mobile不应用样式
【发布时间】:2011-09-26 19:55:58
【问题描述】:

当单击按钮将 ajax 导入的信息放入 ajaxarea1 div 时,我的 phonegap 应用程序会进行简单的 .load 调用。但是,它没有被设置为 jquery mobile 在从 ajax 加载后创建的可折叠类型对象。此处简化js:

$('#ajaxarea1').load('http://server.net/droiddev/backbone1/index.php/welcome/', function(){
    $('#ajaxarea1').css("height","auto");
});

这是 .load 转到 url 时返回的 php

<?php 
    foreach ($query as $row)
    {
         echo '<div data-role="collapsible">';
         echo '<h3>'.$row->headline.'</h3>';
         echo '<p>'.$row->article.'</p>';
         echo '</div>';
    }
?>

我在这里找到的一些类似问题对我没有多大帮助。这是我发现的一些 js,我可能是问题的一部分

$('#ajaxarea1').collapsible().page();

谢谢

完整的 HTML:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Page Title</title> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.js"></script>
     <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.js"></script>
     <script type="text/javascript" charset="utf-8" src="main.js"></script>
     <style type="text/css">  #ajaxarea1{width:308px;
  display:block;
  margin:2px auto;
  height:50px;
  overflow:hidden;} 
  #errorarea ul li{ color:red;}
  .center{text-align:center;}

  </style>
</head> 

<body onload="init()" style=""> 

<!-- Start of first page -->
<div data-role="page" id="firstpage" data-title="Page firstpage" data-theme="d" >

<!--    <div data-role="header" data-theme="d" data-position="fixed">


    </div> -->
    <a href="#" data-role="button" style="margin-top:15px;float:right;" data-iconpos="notext" data-icon="home">Home</a> 

    <div data-role="content">   
        <p>I'm first in the source order so I'm shown as the page.</p>  

        <p>View internal page called <a href="#secondpage" data-role="button" data-theme="d" data-transition="slide">2nd page</a></p>   
     <a href="#addart" data-role="button" data-theme="d" >Add Article</a>

    <div id="ajaxarea1"></div>
    <button type="button" id="calcbtn" onclick="change_ajaxarea1()">Ajax call</button>

    </div><!-- /content -->

    </div>
</div><!-- /page -->


<!-- Start of second page -->
<div data-role="page" id="thanks" data-theme="d">

<p> Your article has been received!!</p>
</div>



<div data-role="page" id="secondpage"  data-theme="d" data-title="Page secondpage ya firstpagel!">

    <div data-role="header" data-theme="d">
        <h1>    <a href="#" data-role="button" style="float:left;" data-rel="back" data-icon="arrow-l" data-theme="d" data-iconpos="notext">Back</a> </h1>

    </div><!-- /header -->

    <div data-role="content">   
        <p>This is the second page of content.</p>      
             <div data-role="collapsible"><!-- this works because its not loaded  by ajax -->
  <h3>Result from Ray</h3>
   <p id="resultBlock"></p>
   </div>


    </div><!-- /content -->

    <!--<div data-role="firstpageter" data-position="fixed">
<p style="width:100%;margin:0 auto;display:block;padding:5px;">The firstpageter is over here and i hope this doesnt get cut off. </p>
    </div> -->

    <!-- /firstpageter -->
</div><!-- /page -->


<!-- Start of third(add) page -->
<div data-role="page" id="addart" data-theme="d" data-title="Page secondpage ya firstpagel!">

    <div data-role="header" data-theme="d">
        <h1>Add article</h1>
        <a href="#" data-role="button" style="float:left;" data-rel="back" data-icon="arrow-l" data-theme="d" data-iconpos="notext">Back</a>
    </div><!-- /header -->

    <div data-role="content">   
        <form id="addform" style="text-align:center;"> <div id="errorarea"><ul>
            </ul></div><label for="Headline">Headline</label><br/><input type="text" name="Headline" title="Enter a title for the Headline at least 5 chars" class="required" id="headline" style="margin: 10px auto 20px auto;" placeholder="enter article headline.."></input>
<br/>   
 <label for="Article">Article</label><br/><textarea cols="40" rows="8" name="Article" id="article" class="required" title="Enter a article" name="textarea" style="margin: 10px auto 20px auto;" placeholder="enter article content.." id="textarea"></textarea>
        <br/>
        <input data-inline="true" style="display:inline-block;clear:both;text-align:center;" type="submit" value="Add Article"  onclick="addarticle()"></input>

        </form>



    </div>



    <!-- /content -->

    <!--<div data-role="firstpageter" data-position="fixed">
<p style="width:100%;margin:0 auto;display:block;padding:5px;">The firstpageter is over here and i hope this doesnt get cut off. </p>
    </div> -->

    <!-- /firstpageter -->
</div><!-- /page -->

</body>
</html>

完整的 Javascript:

function change_ajaxarea1(){


$('#ajaxarea1').load('http://server.net/droiddev/backbone1/index.php/welcome/', function(){
   $('#ajaxarea1').css("height","auto").collapsible();
});



}




function postarticle(){

 $.post("http://server.net/droiddev/backbone1/", { headline: "John", article: "2pm" } );

 }


function addarticle(){
   //$("#addform").validate();

  var truth=$("#addform").validate().form()
 // alert(truth);
if(truth==true){
var headlinetemp=$('#headline').val();
var articletemp=$('#article').val();
navigator.notification.activityStart();
$.ajax({
type:'POST',
url: "http://server.net/droiddev/backbone1/index.php/welcome/addarticle/",
data: { 'headline': headlinetemp, 'article': articletemp},
success:function(){
$('#headline').val('');
$('#article').val('');
 $.mobile.changePage($('#thanks'), { transition: "slide"});
 navigator.notification.activityStop();
},
error:function(xhr){
navigator.notification.activityStop();
alert('Error: Article was not added.');
}



});

}
else{alert('Please Correct the Form');}

}


function init(){
var container = $('#errorarea');
$('#addform').validate({

    errorContainer: container,
        errorLabelContainer: $("ul", container),
        wrapper: 'li',
        meta: "validate"





});

}

function addarticletransition(){

var headlinetemp=$('#headline').val();
var articletemp=$('#article').val();
$.mobile.changePage( "http://server.net/droiddev/backbone1/index.php/welcome/addarticle/", {
    type: "post", 
    data: { 'headline': headlinetemp, 'article': articletemp}
});



}

更新++

【问题讨论】:

    标签: jquery codeigniter jquery-mobile cordova


    【解决方案1】:

    尝试在加载动态内容之后调用collapsible()page() 方法

    $('#ajaxarea1').load('http://server.net/droiddev/backbone1/index.php/welcome/', function(){
        $('#ajaxarea1').css("height","auto").collapsible().page();
    });
    

    【讨论】:

    • $('#ajaxarea1').css("height","auto").collapsible().page();和 $('#ajaxarea1').css("height","auto").collapsible();不起作用。您建议的版本实际上会从页面“屏幕区域”中删除所有内容,而不是按钮本身。另一个版本,即 $('#ajaxarea1').css("height","auto").collapsible();也不更新
    • @CI_Guy:您在原始问题中所说的“简化的 js”是什么意思?您可以发布应用中存在的特定 JS 吗?
    • 向 OP 添加了 html 和 js 源代码。这是 android 上的 phonegap 应用程序,我知道可折叠内容有效,但我无法从 ajax 接收的数据中加载它。再次感谢
    • @CI_Guy:您发布的 JS 是否包含在 main.js 中?
    猜你喜欢
    • 1970-01-01
    • 2014-03-25
    • 2011-12-21
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多