Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript div元素中的childNode_Javascript - Fatal编程技术网

Javascript div元素中的childNode

Javascript div元素中的childNode,javascript,Javascript,我现在正在学习JavaScript,我正在编写函数来更改每个段落标记和字体样式标记强调(em)和粗体(b),它们位于元素中。这是我的程序 <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

我现在正在学习JavaScript,我正在编写函数来更改每个段落标记和字体样式标记强调(em)和粗体(b),它们位于
元素中。这是我的程序

<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>

Lorem ipsum dolor sit amet,献祭精英,
这是临时性的劳动和就业机会。
但是,我们必须尽可能少地进行实验
这是一个很好的例子。在reprehenderit的Duis aute irure dolor
这是一个不可分割的地方。
除非圣约翰无过失,否则必须承担责任
这是我的工作场所

Lorem ipsum dolor sit amet,献祭精英, 这是临时性的劳动和就业机会。 但是,我们必须尽可能少地进行实验 这是一个很好的例子。在reprehenderit的Duis aute irure dolor 这是一个不可分割的地方。 除非圣约翰无过失,否则必须承担责任 这是我的工作场所

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”;
我尝试的是更改div元素中使用的
标记的颜色。但是当我运行输出时,控制台会显示sampDiv2.childNodes[0]。样式未定义

还包括文本节点,因此最好使用返回html元素的

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。酒后驾车
我的心在我的心上,我的心在我的心上。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。酒后驾车 这是一种不受欢迎的行为,它是一种不受欢迎的行为。 除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡

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”;
还包括文本节点,因此最好使用返回html元素的

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。酒后驾车
我的心在我的心上,我的心在我的心上。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡

知识是一种美德,是一种美德,是一种美德,是一种美德。但是,在最低限度上,我们需要一个实验室来进行日常工作。酒后驾车 这是一种不受欢迎的行为,它是一种不受欢迎的行为。 除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡

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”;
使用css,您可以随心所欲

<style>
p    {background-color:#EFDECD;}
em    {color: red;}
b    {color: darkblue;}
</style>
您还可以使用以下方法在纯javascript中获取这些元素:


即使如此,我还是建议更改元素的css类(该类包含所有css信息),而不是直接编辑样式

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

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

.abc {
    /* css */
}
或者您可以更改div的类并在.css中执行以下操作:

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

你可以用css做你想做的事情

<style>
p    {background-color:#EFDECD;}
em    {color: red;}
b    {color: darkblue;}
</style>
您还可以使用以下方法在纯javascript中获取这些元素:


即使如此,我还是建议更改元素的css类(该类包含所有css信息),而不是直接编辑样式

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

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

.abc {
    /* css */
}
或者您可以更改div的类并在.css中执行以下操作:

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

@RSN:check docs,@RSN:check docs,您能告诉我如何为第一个p标记的children[0]中的emphasis标记编写javascript函数吗。我试过了,但是浏览器控制台说这是错误的。sampDiv2.children[0]。children[1]。style.backgroundColor=“#BFAFB2”
document.queryselectoral(“sampdiv2p:first child em”).forEach(function(element){element.style.backgroundColor=“magenta”;})
$(“#sampdiv2p”)[0]。find(“em”).css(“background color”,“gold”)
。这是有效的,你能告诉我如何像我在问题中问的那样写作吗?因为我正在关注一个视频教程,它是为教学人员而不是为我工作的。TypeError:sampDiv2.children[0]。children[1]未定义,但他的语句是sampDiv2.childNode[0]。childNode[1]