简体   繁体   English

如何在不覆盖javascript中的其他内容的情况下动态地向文档添加段落?

[英]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.

相关问题 在HTML / JavaScript中,如何动态设置标题/段落元素的文本而不覆盖其他任何内容? - In HTML/JavaScript, how do you dynamically set a header/paragraph element's text without overwriting anything else? 如何在 Div 中动态添加段落? - How can I add a Paragraph inside a Div dynamically? 如何在此Javascript中添加if / else语句? - How can I add an if/else statement to this Javascript? 如何在文档上的其他内容之前插入元素? - How do I insert an element before everything else on a document? 动态编辑列表项的InnerHtml,而不会覆盖 <li> 标签 - Dynamically edit the InnerHtml of a List Item without overwriting everything in the <li> tag 如何在电子中动态添加段落 - How to add paragraph dynamically in electron 如何动态地将类添加到段落中? - how to dynamically add a class to a paragraph? 如何在没有 splice 方法的情况下向数组添加值并覆盖元素? javascript - How to add a value to an array without the splice method and overwriting the element? javascript 如何在不覆盖Javascript的情况下将一个对象的属性添加到另一个对象中 - How to add properties from one object into another without overwriting in Javascript 我怎样才能:用 JavaScript 选择一个段落(点击)? - How can I: Select a paragraph with JavaScript (on click)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM