2015-10-17 38 views
1

我有这样的代码来显示从DB一些HTML代码:打印成html与jQuery和PHP

$("#menuOverlay").append('<?php include('aggiungiPaginaComp.php');?>'); 

它的工作,从浏览器的源文件:

$("#menuOverlay").append('<form id="aggiungiPagina" 
name="aggiungiPagina" 
action="script/aggiungiPagina.php" 
method="post"> 

<tablestyle="width:100%;"> 
    <tr> 
     <td>NomePagina</td> 
     <td><inputsize="30" 
      maxlength="30" 
      placeholder="Inseriscinomepagina" 
      autofocus 
      required 
      type="text" 
      name="nomePagina" 
      id="nomePagina"></td> 
    </tr> 
    <tr> 
     <td>Descrizione</td> 
     <td><inputsize="50" 
      maxlength="200" 
      placeholder="Inseriscidescrizione" 
      required 
      type="text" 
      name="descrizione" 
      id="descrizione"></td> 
    </tr> 

</table> 
<divid="scrisciaBtnForm"> 
    <inputclass="btnMenu"type="submit"value="invia"/> 
</div> 
</form>'); 

但在div# menuOverlay空白..与:

$("#menuOverlay").append('<Button> ciao </Button>'); 

作品,为什么? 对不起英文不好。

+0

你为什么要追加这样的内容,而不是用PHP渲染它? – PeeHaa

+0

因为div的内容会改变 –

+1

我可以在第一行看到的一件事是,你在单引号内使用单引号。您需要转义这些内容或在单引号内使用双引号。 –

回答

0

有一次,你有很多无效的HTML,比如<tablestyle="width:100%;"> - 我猜想你在标记中损失了很多空间(可能是因为RegEx?),这会破坏你试图插入的HTML。

而且你不能只在JS中有多行字符串。你需要在每一个换行符要么追加\或更换\n使它成为一个字符串连接:

$("#menuOverlay").append('<form id="aggiungiPagina" '+ 
'name="aggiungiPagina" '+ 
'action="script/aggiungiPagina.php" '+ 
'method="post"> '+ 
' '+ 
'<table style="width:100%;"> '+ 
' <tr> '+ 
'  <td>NomePagina</td> '+ 
'  <td><input size="30" '+ 
'   maxlength="30" '+ 
'   placeholder="Inseriscinomepagina" '+ 
'   autofocus '+ 
'   required '+ 
'   type="text" '+ 
'   name="nomePagina" '+ 
'   id="nomePagina"></td> '+ 
' </tr> '+ 
' <tr> '+ 
'  <td>Descrizione</td> '+ 
'  <td><input size="50" '+ 
'   maxlength="200" '+ 
'   placeholder="Inseriscidescrizione" '+ 
'   required '+ 
'   type="text" '+ 
'   name="descrizione" '+ 
'   id="descrizione"></td> '+ 
' </tr> '+ 
' '+ 
'</table> '+ 
'<div id="scrisciaBtnForm"> '+ 
' <input class="btnMenu" type="submit" value="invia"/> '+ 
'</div> '+ 
'</form>'); 

在这里看到:https://jsbin.com/vidajohevi/edit?html,js,output

0

你应该HTML编码的页面在PHP(让你的双引号惯于原因错误),然后html在JS中解码。如果您使用jQuery这应该工作:

String.prototype.htmlDecode = function(){ 
 
    return $('<div/>').html(this).text(); 
 
}; 
 

 
$("#menuOverlay").append("<?php 
 
    ob_start(); 
 
    include('aggiungiPaginaComp.php'); 
 
    $page = ob_get_clean(); 
 
    echo htmlentities($page); 
 
?>".htmlDecode());

0

此:

<tablestyle="width:100%;"> 

应该是:

<table style="width:100%;"> 

同样,这样的:

<divid="scrisciaBtnForm"> 

应该是:

<div id="scrisciaBtnForm"> 

最后,删除aggiungiPaginaComp.php所有新生产线。以这种方式使用Javascript时,Javascript会遇到新行的问题。