2016-06-21 37 views
2
<table id="test"> 
<tbody> 
</tbody> 
</table> 

<script> 

str="<tr><td>3<weeks<=9</td></tr>"; 
$('#test tbody').html('str'); 

</script> 

輸出即將:在表3 作爲一列,因爲「<」將當作HTML標記。如何解決這個問題。我想填充3<周< = 9表如何填充2 <周<9從JavaScript以HTML表格

+0

添加到答案還有很多其他這樣的HTML實體**,如[列在這裏](http://character-code.com/)。 –

回答

1

使用&lt;(或&#60;)爲lessthan(<)符號

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="test"> 
 
    <tbody> 
 
    </tbody> 
 
</table> 
 

 
<script> 
 
    str = "<tr><td>3&lt;weeks&lt;=9</td></tr>"; 
 
    //   --^-- --^-- 
 
    $('#test tbody').html(str); 
 
</script>

參見:http://www.w3.org/TR/html4/sgml/entities.html


或者您可以使用jQuery通過使用jQuery生成元素來完成該任務。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="test"> 
 
    <tbody> 
 
    </tbody> 
 
</table> 
 

 
<script> 
 
    $('<tr>', { 
 
    html: $('<td>', { 
 
     text: '3<weeks<=9' 
 
    }) 
 
    }).appendTo('#test tbody'); 
 
</script>

0

所有你需要在這種情況下做的就是逃避「<」性格,使其不作爲HTML處理。轉義字符HTML代碼'<'在這種情況下是'& lt;'。希望這可以幫助你

+0

謝謝4castle只是自己編輯評論 – BanelingRush