简体   繁体   English

使用javascript在元素内创建元素

[英]Creating elements within elements with javascript

Noob here. Noob在这里。 I searched on the internet a bit to find an answer to a question and I can't seem to find any (and that brings my hope down). 我在互联网上搜索了一些问题的答案,我似乎找不到任何答案(这让我的希望失望)。 So here I am. 所以我在这里。 I was wondering if there is a way to create an HTML element with javascript, BUT inside the newly created HTML element to create also another HTML element with javascript. 我想知道是否有办法用javascript创建一个HTML元素,但是在新创建的HTML元素中还要用javascript创建另一个HTML元素。 I guess you can call it elementception //wink 我想你可以称之为元素// wink

To be more specific, I would like to create a paragraph with text, but I would like to include links in that text (or possibly buttons?). 更具体地说,我想创建一个带有文本的段落,但我想在该文本中包含链接(或者可能包含按钮?)。

var para = document.createElement("P");
var t = document.createTextNode("This is a paragraph. Can I do this: <a href='blabla'>like so?</a>");
para.appendChild(t);
document.body.appendChild(para);

I tried writing HTML tags inside the strings of the TextNode, but even I can see that was stupid of me. 我尝试在TextNode的字符串中编写HTML标记,但即便如此我也能看出这对我来说是愚蠢的。 Is there a noobish(simple) way to achieve this, or any way at all? 是否有一种无用(简单)的方式来实现这一目标,或者任何方式呢? If I'm asking the impossible, please be harsh and blunt about it, so that I never ask questions again. 如果我问不可能的事情,请对此苛刻和直言不讳,以便我再也不会问问题了。 Thanks. 谢谢。

最简单的方法是:

para.innerHTML = 'This is a paragraph. Here is a link: <a href="blabla">like so?</a>';

I would use the DOM API approach instead of using innerHTML for readability, maintainability and security reasons . 我会使用DOM API方法而不是使用innerHTML来实现可读性,可维护性和安全性 Sure innerHTML has been around for a long time, but just because it is easy doesn't mean you should use it for everything. 确实innerHTML已经存在了很长时间,但仅仅因为它很容易并不意味着你应该将它用于一切。

As well, if you're going to be learning JavaScript you should get acquainted with the DOM API sooner than later. 同样,如果您要学习JavaScript,您应该尽早熟悉DOM API。 It will save you a lot of headaches down the road if you get the hang of the API now. 如果您现在掌握API,它将为您节省很多麻烦。

 // Create the parent and cache it in a variable. var para = document.createElement( "p" ); // Create a text node and append it to the child. // We don't need to cache this one because we aren't accessing it again. para.appendChild( document.createTextNode( "This is a paragraph. Can I do this: " ) ); // Create our link element and cache it in a variable. var link = document.createElement( "a" ); // Set the link's href attribute. link.setAttribute( 'href', 'blabla' ); // Create a text node and append it to the link // We don't need to cache the text node. link.appendChild( document.createTextNode( 'like so?' )); // Append the link to the parent. para.appendChild( link ); // Append the parent to the body. document.body.appendChild( para ); 

DOM API methods used: 使用的DOM API方法:

Further reading: 进一步阅读:

Simply use innerHTML attribute to put HTML inside your element instead of createTexteNode , here's what you need: 只需使用innerHTML属性将HTML放在元素而不是createTexteNode ,这就是你需要的:

var para = document.createElement("P");
para.innerHTML = "This is a paragraph. Can I do this: <a \"blabla\">like so?</a>";
document.body.appendChild(para);

Because as its name says, document.createTextNode() will only create a text and can't create HTML elements. 因为正如其名称所示, document.createTextNode()只会创建一个文本而无法创建HTML元素。

 var para = document.createElement("P"); para.innerHTML = "This is a paragraph. Can I do this: <a href=\\"blabla\\">like so?</a>"; document.body.appendChild(para); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM