2014-02-08 129 views
1

是否有可能返回一個特定的div的整個數據和innerhtml或我必須遵循不同的方法..我如何顯示數據到popover div。jquery ajax返回HTML元素

的代碼片段低於 我只發送一個僞值作爲「4」只是爲了檢查,

account.php

在該頁面我使用的酥料餅的顯示內容處理通過AJAX

<a id="popover" href="#" data-trigger="hover" rel="popover" ><img class="img-thumbnail" src="../xxx/xxx/abc.jpg" width="50"/></a> 
<div id="popajax"> 
</div> 

jQuery的

<script type="text/javascript"> 
$(function() 
    { 
     $("#popover").mouseover(function() 
     { 
      var textcontent = '4'; 
      $.ajax({ 
        type:'GET', 
        url: "/getuserdetails.php", 
        data: {myval:textcontent}, 
        cache: true, 
        error: function(){ 
         alert ("ERROR"); 
        }, 
        success: function(html) 
        { 
         $("#popajax").after(html); 
        } 
       }); 
      return false; 

     }); 
    }); 
</script> 

酥料餅的腳本

<script> 
$(function() { 
$("#popover").popover({ trigger: "hover", 
html: true, 
placement : 'top', 
    content: function() { 
     return $('#popover_content').html(); 
    }, 
    title: function() { 
     return $('#popover_title').html(); 
    } 
}); 
}); 
</script> 

getuserdetails.php

<?php session_start(); 
include('include/functions.php'); /* This is for crud and other functions*/ 

    if(isset($_GET['myval'])) 
    { 
    $sql = sql::readOne("SELECT * FROM table_name WHERE id='$_GET[myval]'"); 
    } 
?> 

<div id="popover_title" class="hide" > 
      <h4><?php echo $sql[0]->username ?></h4> 
      </div> 
      <div id="popover_content" class="hide" > 

      <div> <p> This is Atul Joshi</p></div> 
    </div> 

*像這樣

enter image description here

回答

3

如果您想將您的結果裏面<div id="popajax"> </div>

您可以使用html()函數來插入元素。

success: function(data){ 
    $("#popajax").html(data); 
} 

您可以使用此函數獲取特定元素的html。像

$(element).html() 

或者如果你想獲得特定元素的內部文本,那麼你可以使用。

$(element).text() 
1

接收html代碼並顯示爲html文本。 您應該使用.html()

$('SELECTOR').html('<STRONG>html data</STRONG>'); 

要顯示爲文本,簡單.text()足夠

$('*SELECTOR*').text('your text goes here'); 

這些都是爲選擇設定值。

將HTML或文本從選擇jQuery中

簡單$('SELECTOR').html(); $('SELECTOR').text();做的工作。