将li元素插入到特定div的ul中

时间:2015-06-02 16:33:28

标签: javascript html insert html-lists

我想使用JavaScript将新的li元素插入到现有的ul中。新的li元素将是ul中的最后一项。

<div id="foo">
   <div>
     <ul>
       <li></li>
       <li></li>
       ....insert html here....
     </ul>    
   </div>
</div>

2 个答案:

答案 0 :(得分:1)

应该像

一样简单
if(ps == null) {
        //'-logfn','/dev/null', 
        //sudo pocketsphinx_continuous -dict lm/8531.dic -lm lm/8531.lm -kws words.kws  -kws_threshold 1e-40 -logfn /dev/null -inmic yes
        //console.log(process.stdout.write(''));


        ps = spawn('pocketsphinx_continuous', ['-nfft','2048', '-hmm','/usr/local/share/pocketsphinx/model/en-us/en-us', '-dict','lm/8531.dic', '-lm','lm/8531.lm', '-kws','words.kws', '-kws_threshold','1e-40', '-inmic', 'yes']);
        
        //ps = spawn('bash',['test.sh']);
        //ps.stderr.pipe(process.stdout);
        //console.log(ps.stdout.write(''));

        ps.stdout.on('data', function (data) {
          console.log('stdout: ' + data);
        });
        ps.stderr.on('data', function (data) {
          console.log('stderr: ' + data);
        });
    }

或在jQuery中

var ul = document.querySelector('#foo ul');
var li = document.createElement('li');

li.innerHTML = 'new LI';
li.className = 'someclass';

ul.appendChild(li);

答案 1 :(得分:0)

尝试“.append()”

$('#foo ul').append( '<li />' );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="foo">
   <div>
     <ul>
       <li></li>
       <li></li>
       ....insert html here....
     </ul>    
   </div>
</div>