Yahoo 知識+ 將於 2021 年 5 月 4 日 (美國東岸時間) 停止服務,而 Yahoo 知識+ 網站現已轉為僅限瀏覽模式。其他 Yahoo 資產或服務,或你的 Yahoo 帳戶將不會有任何變更。你可以在此服務中心網頁進一步了解 Yahoo 知識+ 停止服務的事宜,以及了解如何下載你的資料。
Dynamically adding text to a div tag using Javascript???
Ok, I have been working on a website for school and I am working on parsing an XML page into a table in an HTML page using Javascript. So far, I have been able to successfully parse the data onto the page. Now, for the past few days, I have been working on selecting a single group from a drop down list. The drop down list loads just fine with the proper text and it can distinguish one group but I am having some trouble getting the text back on the page. Right now it will blank the page and come up with a white background and it isn't aligned. I thought I could drop the code in to a div tag, but I am having some trouble. I am parsing the data and using "document.write()" to display it. I want everything that is written by document.write to be displayed in a div tag so the data doesn't come up in a new page.
<div align="center">
<script type="text/javascript">
// Navyear is defined earlier.
populateAwards(navYear.options[navYear.selectedIndex].text);
</script>
</div>
Yeah, but I am trying to load it INTO the div tag. If I did put the div tag, then it would once again load the white background page, except the data would be centered.
By the way, it is not just 1 line of data I want to enter, It is a large table.
3 個解答
- richarduieLv 61 十年前最愛解答
Avoid document.write(); it's destructive of other content unless managed carefully. Try either DOM operations such as appendChild() or direct manipulation of the innerHTML property of the target container div. In the following example, get() and create*() methods are just shorthand names to let the lines display in YA. Replace "break" with "br" in the catenate function.
<html>
<head>
<!--
No matter which one you select,
pick a Best Answer.
-->
<script type="text/javascript">
var xml = [
'xml content, line 1',
'xml content, line 2',
'xml content, line 3',
'xml content, line 4'
]
function get(eid) {
var d = document;
var r = d.getElementById(eid);
return r;
}
function createTN(t) {
var d = document;
var r = d.createTextNode(t);
return r;
}
function createEl(e) {
var d = document;
var r = d.createElement(e);
return r;
}
function appendXml() {
var x = get('xdiv');
var f = 'from append: ';
for (var i = 0; i < xml.length; i++) {
x.appendChild(createTN(f + xml[i]));
x.appendChild(createEl('BR'));
}
}
function catenateXml() {
var x = get('xdiv');
var f = 'from catenate: ';
for (var i = 0; i < xml.length; i++) {
x.innerHTML += f + xml[i] + '<break />';
}
}
function init() {
appendXml();
catenateXml();
}
window.onload = init;
</script>
</head>
<body>
<p>original, base content goes here</p>
<div id="xdiv">
<p>xml will be added below here</p>
</div>
</body>
</html>
- Jim HLv 51 十年前
Include the <div> and </div> tags in the document.write() statement.
For example. the variable x has the HTML you want to write:
document.write('<div>'+x+'</div>')