最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display document.write in a specific div with specific id? - Stack Overflow

programmeradmin2浏览0评论

I want to display my document.write("something important");

into a specific div with specific id.

Im my HTML, I have a div <div class="col-lg-4" id="print"> ... </div>

In my JavaScript, I have a loop.

for(var i = 0; i < L; i++) {

  document.write(

    navigator.plugins[i].name +
    " | " +
    navigator.plugins[i].filename +
    " | " +
    navigator.plugins[i].description +
    " | " +
    navigator.plugins[i].version +
    "<br><hr><br>"
  );
}

What is the most efficient way to display them in my div ?

I want to display my document.write("something important");

into a specific div with specific id.

Im my HTML, I have a div <div class="col-lg-4" id="print"> ... </div>

In my JavaScript, I have a loop.

for(var i = 0; i < L; i++) {

  document.write(

    navigator.plugins[i].name +
    " | " +
    navigator.plugins[i].filename +
    " | " +
    navigator.plugins[i].description +
    " | " +
    navigator.plugins[i].version +
    "<br><hr><br>"
  );
}

What is the most efficient way to display them in my div ?

Share Improve this question edited Mar 18, 2015 at 19:05 iori asked Mar 18, 2015 at 18:46 ioriiori 3,50615 gold badges49 silver badges79 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I would remend not using document.write, especially after the page has loaded. It can lead to unexpected results. Just use this method:

document.getElementById('print').innerHTML = "something important";

If, however, you did not want to replace the whole innerHTML, you could append something to it:

document.getElementById('print').insertAdjacentHTML('beforeend', "something added");

Update

Here is an example with a loop:

var elem = document.getElementById('print'),
    L = navigator.plugins.length;

for(var i = 0; i < L; i++) {
  elem.insertAdjacentHTML('beforeend',
        navigator.plugins[i].name +
        " | " +
        navigator.plugins[i].filename +
        " | " +
        navigator.plugins[i].description +
        " | " +
        navigator.plugins[i].version +
        "<br><hr><br>"
  );
}

JS Fiddle Demo

man! I think that what you want to do is update the content of the element, right? If so: document.getElementById('print').innerHTML = something important

I hope I have helped you

发布评论

评论列表(0)

  1. 暂无评论