2014-02-08 131 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();做的工作。