2017年12月6日水曜日

[jQuery] 要素の直下にコンテンツを追加したい/Insert content to the beginning of each element

<問題・Problem>

コンテンツをappendで追加しようとすると、要素の一番最後に追加されるが、一番最初に追加したい。
You want to add contents at the beginning of the selected element. But when you use "append", contents will be insert at the end of the selected element.


<解決策・Solutions>

appendではなくprependを使う。
Use "prepend" instead of "append".


<サンプル・Sample>

 HTML :   
 <ul>  
   <li>pen</li>  
   <li>pineapple</li>  
   <li>apple</li>  
 </ul>  
   
 jQuery :  
 $('li').prepend('<li>PPAP!</li>');  
   
 Result :   
 <ul>  
   <li>PPAP</li>  
   <li>pen</li>  
   <li>pineapple</li>  
   <li>apple</li>  
 </ul>  
   

0 件のコメント:

コメントを投稿