简体   繁体   English

div元素内的childNode

[英]childNode inside a div element

I'm learning the JavaScript right now and I'm writing functions to change the style of the each paragraph tag and the font-style tag emphasis(em) and bold(b) which is inside the <div> element. 我现在正在学习JavaScript,并且正在编写函数来更改每个段落标签的样式以及<div>元素内部的font-style标签强调(em)粗体(b) Here is my program . 这是我的程序

<div id = "sampDiv2">
<p>Lorem ipsum dolor sit amet, <em>consectetur adipiscing</em> elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi 
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit 
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui 
officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, <b>consectetur adipiscing</b> elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi 
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit 
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
<em>Excepteur sint</em> occaecat cupidatat non proident, <b>sunt in culpa</b> qui 
officia deserunt mollit anim id est laborum.</p>
</div>

<script>
var pElements = document.getElementsByTagName('p');
pElements[1].style.backgroundColor = "#EFDECD";

document.childNodes[1].style.backgroundColor = "#FAEBD7";

var sampDiv2 = document.getElementById('sampDiv2');

sampDiv2.childNodes[0].style.backgroundColor = "#F0FFFF";
</script>

what I'm trying is to change the color of the <p> tags used inside the div element. 我正在尝试的是更改div元素中使用的<p>标记的颜色。 But when I run the output, the console says sampDiv2.childNodes[0].style is undefined 但是,当我运行输出时,控制台显示sampDiv2.childNodes [0] .style未定义

The childNodes includes text nodes also, so it's better to use children instead which returns html elements. childNodes包括文本节点,因此最好使用children而不是返回html元素的children

 <div id="sampDiv2"> <p>Lorem ipsum dolor sit amet, <em>consectetur adipiscing</em> elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Lorem ipsum dolor sit amet, <b>consectetur adipiscing</b> elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <em>Excepteur sint</em> occaecat cupidatat non proident, <b>sunt in culpa</b> qui officia deserunt mollit anim id est laborum.</p> </div> <script> var pElements = document.getElementsByTagName('p'); pElements[1].style.backgroundColor = "#EFDECD"; document.body.children[1].style.backgroundColor = "#FAEBD7"; var sampDiv2 = document.getElementById('sampDiv2'); sampDiv2.children[0].style.backgroundColor = "#F0FFFF"; </script> 

You can do what you want with css. 您可以使用CSS来做您想做的事情。

<style>
p    {background-color:#EFDECD;}
em    {color: red;}
b    {color: darkblue;}
</style>

With this all <p> element will have that background colors, em elements will be red and bold elements will be dark blue. 这样,所有<p>元素将具有该背景色, em元素将为红色,粗体元素将为深蓝色。 Learn more about css to know more. 了解有关CSS的更多信息。

By the way, the code above works if put in the html file, but it's recommended to put in in a separate css file (with <style> removed) and use that css file in your html file. 顺便说一句,如果将上述代码放入html文件,则可以工作,但是建议将其放入单独的css文件(已删除<style> )并在html文件中使用该css文件。

If you want to easily select elements, you can do this with jQuery : 如果您想轻松选择元素,则可以使用jQuery来做到这一点:

//Targets all descendendants of the element with id #sampDiv2 that are 'p' tags
$("#sampDiv2 p").css({"background-color": "#F0FFFF"});

You can also get those elements in pure javascript using document.querySelectorAll : 您还可以使用document.querySelectorAll在纯JavaScript中获取这些元素:

//nodes is an array with all the elements in it
var nodes = document.querySelectorAll("#sampDiv2 p");

//To change the first one, but then document.querySelector() would have been enough
nodes[0].style.backgroundColor = "#F0FFFF";

//To change all
for (let node of nodes) {
    node.style.backgroundColor = "#F0FFFF";
}

//Alternative in case above is not yet supported:
nodes.forEach(function(node) {
    node.style.backgroundColor = "#F0FFFF";
});

Even then, I'd recommend changing the css class of your elements (with the class containing all the css info) rather than editing the style directly. 即使这样,我还是建议您更改元素的css类(该类包含所有css信息),而不是直接编辑样式。

//jQuery way
$("#sampDiv2 p").addClass("abc");

//alternative
for (let node of document.querySelectorAll("#sampDiv2 p")) {
    node.classList.add("abc");
}

In the css: 在CSS中:

.abc {
    /* css */
}

Or you can change the class of your div and do in the .css: 或者,您可以更改div的类并在.css中执行以下操作:

.myDivClass p {
   /* css for p tags under those div */
}

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

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