我使用querySelectorAll到我的index.html文件中访问ID和类。那我遇到的问题是,我试图将数据传递到其中有两个孩子跨度我p.source课,当我使用的innerText或innerHTML的数据没有被传递到DOM尽管我的控制台日志显示这意味着正确的数据传递到DOM。你能帮助我理解它是什么,发生的事情以及我如何将数据传递到源类。我是新来使用querySelector和MDN文档不是安静的深度,因为我想。下面是我的代码。
到目前为止,使用控制台我试图访问childeNodes的性质和他们的工作正确的刚开的数据到DOM中得到体现
function myQuotes() {
var quotes = [{
quote: "Seven billion souls that move around the sun.",
source: "Childish Gambino",
citation: "Feels like summer"
},
{
quote: "Don't wait for your world to begin crumbling to make a change.",
source: "Josh",
citation: "Past midnight thoughts"
},
{
quote: "The only time we will ever have is right now, no other time exist.",
source: "Josh",
citation: "Past midnight thoughts",
year: 2018
},
{
quote: "Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.",
source: "Patrick McKenzie",
citation: "Twitter"
}
];
console.log('quotes', quotes);
return quotes;
}
var myQuotesLength = myQuotes().length;
var quotes = myQuotes();
function getRandomQuote(maxNum) {
var randomNum = Math.floor(Math.random() * maxNum);
var randomQuote = quotes[randomNum];
// console.log('random quote', randomQuote );
return randomQuote;
}
function printQuote() {
//data call to grab quotes from array of objects
var message = getRandomQuote(myQuotesLength);
console.log('message', message);
//using querySelectors to dynamically pass the data from 'message' into the DOM. Each field works except for sourceField.
var quoteField = document.querySelectorAll('.quote')[0].innerText = message.quote;
var source_Parent = document.querySelectorAll('.source');
console.log('parent', source_Parent);
var sourceField = source_Parent[0].childNodes[0].innerHTML = message.source;
console.log('source', sourceField);
var citationField = source_Parent[0].childNodes[1].innerText = message.citation;
var yearField = source_Parent[0].childNodes[2].innerText = message.year;
if (yearField === undefined) {
yearField = document.getElementsByClassName('year')[0].innerHTML = ' ';
}
}
document.getElementById('loadQuote').addEventListener("click", printQuote, false);
body {
background-color: #36b55c;
color: white;
font-family: 'Playfair Display', serif;
}
#quote-box {
position: absolute;
top: 20%;
left: 10%;
right: 10%;
width: 80%;
line-height: .5;
}
.quote {
font-size: 4rem;
font-weight: 400;
line-height: 1.1;
position: relative;
margin: 0;
}
.quote:before,
.quote:after {
font-size: 6rem;
line-height: 2.5rem;
position: absolute;
}
.quote:before {
content: "“";
top: .25em;
left: -.5em;
}
.quote:after {
content: "”";
bottom: -.1em;
margin-left: .1em;
position: absolute;
}
.source {
font-size: 1.25rem;
;
letter-spacing: 0.05em;
line-height: 1.1;
text-align: right;
margin-right: 4em;
}
.source:before {
content: "—";
}
.citation {
font-style: italic;
}
.citation:before {
content: ", ";
font-style: normal;
}
.year:before {
content: ", ";
font-style: normal;
}
#loadQuote {
position: fixed;
width: 12em;
display: inline-block;
left: 50%;
margin-left: -6em;
bottom: 150px;
border-radius: 4px;
border: 2px solid #fff;
color: #fff;
background-color: #36b55c;
padding: 15px 0;
transition: .5s;
}
#loadQuote:hover {
background-color: rgba(255, 255, 255, .25);
}
#loadQuote:focus {
outline: none;
}
@media (max-width: 420px) {
.quote {
font-size: 2.5rem;
}
.quote:before,
.quote:after {
font-size: 3rem;
}
.source {
font-size: 1rem;
}
}
<link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
<div class="container">
<div id="quote-box">
<p class="quote">Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.</p>
<p class="source">Patrick McKenzie<span class="citation">Twitter</span><span class="year">2016</span></p>
</div>
<button id="loadQuote">Show another quote</button>
</div>
最终的结果是:
-I'd喜欢的message.quote数据传递到报价盒ID的innerHTML(这工作)
-I'd喜欢的message.source数据传递到源类的的innerText(不工作)
-I'd喜欢的message.citation数据传递到引用类的的innerText(这个作品)
-I'd喜欢的message.year数据传递到一年类的innerText(这工作)
你面临的名字作为childNode,但它不是.source div
的childNode,它的innerText
。如果你把名字变成<span>
过,它会成为一个childNode过和将工作就像一个魅力。
这是你的“固定” codepen,在我刚加入周围的名称
<span>
:https://codepen.io/anon/pen/pGrBEo?editors=1010#0
还是来看看这个工作的例子:
function myQuotes() {
var quotes = [{
quote: "Seven billion souls that move around the sun.",
source: "Childish Gambino",
citation: "Feels like summer"
},
{
quote: "Don't wait for your world to begin crumbling to make a change.",
source: "Josh",
citation: "Past midnight thoughts"
},
{
quote: "The only time we will ever have is right now, no other time exist.",
source: "Josh",
citation: "Past midnight thoughts",
year: 2018
},
{
quote: "Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.",
source: "Patrick McKenzie",
citation: "Twitter"
}
];
return quotes;
}
var myQuotesLength = myQuotes().length;
var quotes = myQuotes();
function getRandomQuote(maxNum) {
var randomNum = Math.floor(Math.random() * maxNum);
var randomQuote = quotes[randomNum];
return randomQuote;
}
function printQuote() {
var message = getRandomQuote(myQuotesLength);
var quoteField = document.querySelectorAll('.quote')[0].innerText = message.quote;
var source_Parent = document.querySelectorAll('.source');
var sourceField = source_Parent[0].childNodes[0].innerHTML = message.source;
var citationField = source_Parent[0].childNodes[1].innerText = message.citation;
var yearField = source_Parent[0].childNodes[2].innerText = message.year;
if (yearField === undefined) {
yearField = document.getElementsByClassName('year')[0].innerHTML = ' ';
}
}
document.getElementById('loadQuote').addEventListener("click", printQuote, false);
body {
background-color: #36b55c;
color: white;
font-family: 'Playfair Display', serif;
}
#quote-box {
position: absolute;
top: 20%;
left: 10%;
right: 10%;
width: 80%;
line-height: .5;
}
.quote {
font-size: 4rem;
font-weight: 400;
line-height: 1.1;
position: relative;
margin: 0;
}
.quote:before,
.quote:after {
font-size: 6rem;
line-height: 2.5rem;
position: absolute;
}
.quote:before {
content: "“";
top: .25em;
left: -.5em;
}
.quote:after {
content: "”";
bottom: -.1em;
margin-left: .1em;
position: absolute;
}
.source {
font-size: 1.25rem;
;
letter-spacing: 0.05em;
line-height: 1.1;
text-align: right;
margin-right: 4em;
}
.source:before {
content: "—";
}
.citation {
font-style: italic;
}
.citation:before {
content: ", ";
font-style: normal;
}
.year:before {
content: ", ";
font-style: normal;
}
#loadQuote {
position: fixed;
width: 12em;
display: inline-block;
left: 50%;
margin-left: -6em;
bottom: 150px;
border-radius: 4px;
border: 2px solid #fff;
color: #fff;
background-color: #36b55c;
padding: 15px 0;
transition: .5s;
}
#loadQuote:hover {
background-color: rgba(255, 255, 255, .25);
}
#loadQuote:focus {
outline: none;
}
@media (max-width: 420px) {
.quote {
font-size: 2.5rem;
}
.quote:before,
.quote:after {
font-size: 3rem;
}
.source {
font-size: 1rem;
}
}
<link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
<div class="container">
<div id="quote-box">
<p class="quote">Every great developer you know got there by solving problems they were unqualified to solve until they actually did it.</p>
<p class="source"><span>Patrick McKenzie</span><span class="citation">Twitter</span><span class="year">2016</span></p>
</div>
<button id="loadQuote">Show another quote</button>
</div>
问题是您正在使用childNodes
还包括文本。
下面是从MDN记
childNodes
包括所有子节点,包括像文字和注释节点非元素节点。要获得唯一元素的集合,而是使用ParentNode.children
。
你可以用createTextNode
和insertBefore
方法做到这一点,如:
const source = message.source;
const sourceNode = document.createTextNode(source);
const sourceEl = document.querySelectorAll(".source")[0];
const citationEl = document.querySelectorAll(".citation")[0];
sourceEl.insertBefore(sourceNode, citationEl);