[英]How can I dynamically add a paragraph to a document without overwriting everything else in javascript?
I'm just learning how to write javascript, but i've already learned java. 我只是在学习如何编写javascript,但我已经学习了java。 the difference in syntax is driving me nuts.
语法上的差异让我疯狂。 i'm just doing a dumb little sample code to see if i can add a paragraph to the end of a page at the click of a button.
我只是做一个愚蠢的小样本代码,看看我是否可以点击一个按钮将一个段落添加到页面的末尾。 but i can't seem to do it.
但我似乎无法做到这一点。 i'm positive that i'm really close to achieving my goal though.
我很肯定我真的很接近实现我的目标。 i already know the button functions properly.
我已经知道按钮功能正常了。 there is a similar post that has helped me, but i can't seem to quite get to where i want.
有一个类似的帖子帮助了我,但我似乎无法到达我想要的地方。 here's the javascript:
这是javascript:
function displayDate()
{
//object that will hold newParagraph
var iframe = document.createElement("iframe");
iframe.width = 400;
iframe.height += 50;
iframe.id ="iframe";
var comment = "woohooo! go me.";
//var doc = iframe.contentDocument || iframe.document;
var newParagraph = document.createElement('p');
newParagraph.textContent = comment;
//doc.body.appendChild(newParagraph);
iframe.appendChild(newParagraph);
document.getElementById("updateDiv").appendChild(iframe);
//the id updateDiv is in my html
}
the iframe shows up as a blank box without the text. iframe显示为没有文本的空白框。 i cannot figure out how to get the text to appear in the iframe.
我无法弄清楚如何让文本出现在iframe中。
here's the similar post if you're curious: Adding iframe and paragraph elements dynamically 如果你很好奇,这里是类似的帖子: 动态添加iframe和段落元素
If you just want to add a paragraph to the document, ditch the iframe. 如果您只想在文档中添加一个段落,请抛弃iframe。 An iframe embeds a completely new document inside your browser window.
iframe在浏览器窗口中嵌入了一个全新的文档。 For your purposes, simply appending the
<p>
element onto the <div id='updateDiv'>
should be enough: 为了您的目的,只需将
<p>
元素附加到<div id='updateDiv'>
就足够了:
function displayDate() {
var comment = "woohooo! go me.";
var newParagraph = document.createElement('p');
newParagraph.textContent = comment;
document.getElementById("updateDiv").appendChild(newParagraph);
}
and here's the HTML: 这是HTML:
<button id='a'>Click Me!</button>
<div id='updateDiv'></div>
And here's the fiddle example: http://jsfiddle.net/MYy72/ 这里是小提琴的例子: http : //jsfiddle.net/MYy72/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.