简体   繁体   English

JavaScript-如何在div中打印p元素

[英]JavaScript- how to print p element inside div

I Know I can do this with static HTML, but I want to create dynamically, and I am struggling a lot. 我知道我可以用静态HTML做到这一点,但我想动态创建,而且我正在苦苦挣扎。 Here is what I want to do: I have 2 divs. 这是我想要做的:我有2个div。

    <div class="TxtTile">

    </div>
    <div class="pInfo">

    </div>

Inside each div I want to have several paragraphs. 在每个div里面我想要几个段落。 Let's say 10, in each div. 让我们说每个div中有10个。 The first div with class "TxtTile" i want to have the title of something, let's say titles like, age,country,experience,street etc.In the other div with class 'pInfo' I want to contain the information that corresponds with TxTtitle. 带有"TxtTile"类的第一个div我希望得到一些东西的标题,让我们说像年龄,国家,经验,街道等标题。在另一个带有'pInfo''pInfo' div中我想要包含与TxTtitle对应的信息。 Like, age 25, experience 10 years etc, that will be taken from local Storage, where I already have it set up. 比如,25岁,经历10年等,将从我已经设置的本地存储中获取。 This two divs will be next to each other, which I have already done with css. 这两个div将是彼此相邻的,我已经用css完成了。

For example. 例如。 Left side 左边

<div class="TxtTile">               `<div class="pInfo">

 <p class="styleforP">                   <p class="styleforP">
    Age                                      25
                                         </p>
 </p>

</div>                                </div>`

I would be happy if I can make this with native js. 如果我能用本机js做这个,我会很高兴。

There are two ways you can do this: 有两种方法可以做到这一点:

1) you can create an element and keep appending to its place 1)你可以创建一个元素并继续追加它的位置

First get div element inside which you want to create new element, Here rather than having a class i would prefer to have id based selection of the element 首先得到你要创建新元素的div元素,这里不是有一个类我更喜欢基于id的元素选择

var element = document.querySelector('.TxtTile');

Create a p element and add class to it, you can similarly add content inside it aswell 创建一个p元素并向其添加类,您也可以在其中添加内容

var pElem = document.createElement('p');
pElem.className = 'styleforP';
pElem.innerHTML = 'Age';

Append that created element inside your div 在div中添加创建的元素

element.appendChild(pElem);

2) Create an HTML template pass your values to that template and create innerHTML and directly put that innerHTML into your parent element 2)创建一个HTML模板将值传递给该模板并创建innerHTML并直接将innerHTML放入您的父元素中

var item = {
    name: "My Name",
    age: 30,
    other: "Other Info"
}
var template = [];

template.push(
    '<div class="row">',
        '<span class="name-info">' + item.name + '</span>',
        '<span class="age-info">' + item.age + '</span>',
        '<span class="other-info">' + item.other + '</span>',
    '</div>'
);

var htmlString = template.join('');
var element = document.querySelector('.TxtTile');
element.innerHTML = htmlString;

If you are going to add a lot of items then second approach is a lot better, as creating single element and appending them to DOM tree is quite slow, then passing whole HTML string. 如果你要添加很多项目,那么第二种方法要好得多,因为创建单个元素并将它们附加到DOM树是非常慢的,然后传递整个HTML字符串。

 var myData = { title: "My title", info: 25 }; // Store references to the wrapper elements // The first element that has this class, or null if there aren't any var titleWrapper = document.querySelector(".js-titleWrapper"); var infoWrapper = document.querySelector(".js-infoWrapper"); // Create the paragraph elements var titleP = document.createElement("p"); var infoP = document.createElement("p"); // Add classes titleP.classList.add("styleForP"); infoP.classList.add("styleForP"); // Add the text titleP.innerText = myData.title; infoP.innerText = myData.info; // Add the paragraphs to their wrappers titleWrapper.appendChild(titleP); infoWrapper.appendChild(infoP); 
 <div class="TxtTile js-titleWrapper"> </div> <div class="pInfo js-infoWrapper"> </div> 

i think this is a bad idea for doing this if you have multiple records then you cant handle by querySlector. 我认为如果你有多个记录然后你不能通过querySlector来处理这个是一个坏主意。

good idea is create a parent element like this 好主意是创建这样的父元素

<div id="parent"></div>

then get this element by javascript and then append this element with your dynamic records like this 然后通过javascript获取此元素,然后将此元素附加到您的动态记录中

var parentEle = document.getElementById("parent");

apply loop if records are multiple 如果记录是多个,则应用循环

var child = Document.createElement("div");
child.innerHTML = "<div class='TxtTile'>age</div><div class='pInfo'>25</div>";
parentEle.appendChild(child);

如何访问<p>三层里面</p><div>使用 Javascript?</div><div id="text_translate"><p> 我正在尝试使用 Javascript,这样当用户按下网页上的按钮时,一个段落将在两个文本之间交替。 问题是我要操作的<p>元素位于<div>内的<div>内的<div> div> 内。</p><p> 我的代码的低级模型如下所示:</p><pre class="lang-html prettyprint-override"> <div id="div1"> <div id="div2"> <div id="div3"> <p>text1</p> </div> </div> </div></pre><p> 我发现的所有解决方案只有 state 如果<p>在一个<div>内,该怎么办; 对我不起作用的解决方案。</p><p> 这是我最近的尝试:</p><pre class="lang-js prettyprint-override"> function translate() { var x = document.getElementById("div1").getElementById("div2").getElementById("div3").getElementsByTagName('p')[0]; if (x.innerHTML === "text1") { x.innerHTML = "text2"; } else { x.innerHTML = "text1"; } }</pre><p> 我怎样才能让它发挥作用?</p><p> 编辑:到目前为止似乎没有任何效果。 <div>元素都有类,但这应该不会影响事情,对吧?</p><p> Edit2:很抱歉占用您的时间,但我发现这个问题完全是另外一回事。 我一直在为我的 function 调用使用保留字,但不知何故从来没有注意到这是问题所在。 再次感谢那些回答的人,在发布下一个问题之前,我将更深入地研究我的代码。</p></div> - How to access a <p> inside three layers of <div> using Javascript?

暂无
暂无

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

相关问题 添加一个 <p> javascript中的DIV中的元素 - Adding a <p> element inside a DIV with javascript JavaScript-如何将“ message” div更改为黄色 - JavaScript- How to change “message” div to yellow 如何在其中打印带有 canvas 的 div 元素 - How to print div element with canvas inside it Javascript-如何在单击关闭按钮时显示div? - Javascript- How to show div when close button is clicked? JavaScript-如何在数组中递归地查找元素并返回其路径? - JavaScript- How to find an element and return its path in an array recursively? javascript-如何通过鼠标位置分离特定元素? - javascript- How can detach specific element by mouseposition? 在JavaScript中使用createElement,如何添加 <p> <span>在里面</span> <div> <span>?</span> - Using createElement in JavaScript, how to add <p> and <span> inside of <div>? 如何访问<p>三层里面</p><div>使用 Javascript?</div><div id="text_translate"><p> 我正在尝试使用 Javascript,这样当用户按下网页上的按钮时,一个段落将在两个文本之间交替。 问题是我要操作的<p>元素位于<div>内的<div>内的<div> div> 内。</p><p> 我的代码的低级模型如下所示:</p><pre class="lang-html prettyprint-override"> <div id="div1"> <div id="div2"> <div id="div3"> <p>text1</p> </div> </div> </div></pre><p> 我发现的所有解决方案只有 state 如果<p>在一个<div>内,该怎么办; 对我不起作用的解决方案。</p><p> 这是我最近的尝试:</p><pre class="lang-js prettyprint-override"> function translate() { var x = document.getElementById("div1").getElementById("div2").getElementById("div3").getElementsByTagName('p')[0]; if (x.innerHTML === "text1") { x.innerHTML = "text2"; } else { x.innerHTML = "text1"; } }</pre><p> 我怎样才能让它发挥作用?</p><p> 编辑:到目前为止似乎没有任何效果。 <div>元素都有类,但这应该不会影响事情,对吧?</p><p> Edit2:很抱歉占用您的时间,但我发现这个问题完全是另外一回事。 我一直在为我的 function 调用使用保留字,但不知何故从来没有注意到这是问题所在。 再次感谢那些回答的人,在发布下一个问题之前,我将更深入地研究我的代码。</p></div> - How to access a <p> inside three layers of <div> using Javascript? Javascript创建元素作为另一个元素的子元素 - Javascript- creating element as a child of another element 如何 select 和切换 javascript 中 div 内的元素 - How to select and toggle element inside the div in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM