gpt4 book ai didi

javascript - 根据 Javascript 使用 HTML 标签的问题

转载 作者:行者123 更新时间:2023-11-28 01:13:34 25 4
gpt4 key购买 nike

这是我的代码,可以完美运行,但仅适用于单引号(稍后引用):

<section class="quote">
<div class="content">
<h4>Bruce Lee</h4>
<p>Ever since I was a child I have had this instinctive urge for expansion and growth. To me, the function and duty of a quality human being is the sincere and honest development of one's potential.</p>
</div>
</section>

不过我想包括一个随机报价生成器。我已经有了脚本和以下内容:

<section class="quote">
<div class="content">
<p><script language="JavaScript">
/* Copyright 2004 by CodeLifter.com */
var Quotation=new Array()

Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script></p>
</div>
</section>

问题:我想根据标签 <p> 将生成随机引文的脚本与其作者相关联报价和<h4>对于作者(在我的第一组代码中引用)。然后我很难弄清楚我将如何做到这一点。

我将如何编写生成随机引用并包含作者的脚本?我将如何用它们的特定标签来定义它们?

会不会是这样的:

Quotation[0] = "Why must we question pancakes?";
Author[0] = "Sigmund Freud";

但现在我如何让它们与它们的特定标签相关联:报价同<p>使用 <h4> 标记和作者标签。

特别是报价部分。

更新:

 <section class="quote">
<div class="content">
<h4></h4>
<p></p>

<script language="JavaScript">
/* Copyright 2004 by CodeLifter.com */
var Quotation = [];

Quotation[0] = ["example Author 1", "Time is of the essence! Comb your hair."];
Quotation[1] = ["example Author 2", "Sanity is a golden apple with no shoelaces."];
Quotation[2] = ["example Author 3", "Repent! The end is coming, $9.95 at Amazon."];
Quotation[3] = ["example Author 4", "Honesty blurts where deception sneezes."];
Quotation[4] = ["example Author 5", "Pastry satisfies where art is unavailable."];
Quotation[5] = ["example Author 6", "Delete not, lest you, too, be deleted."];
Quotation[6] = ["example Author 7", "O! Youth! What a pain in the backside."];
Quotation[7] = ["example Author 8", "Wishes are like goldfish with propellors."];
Quotation[8] = ["example Author 9", "Love the river's \"beauty\", but live on a hill."];
Quotation[9] = ["example Author 10", "Invention is the mother of too many useless toys."];

var Q = Quotation.length;
var whichQuotation = Math.round(Math.random() * (Q - 1));

document.querySelector('h4').textContent = Quotation[whichQuotation][0];
document.querySelector('p').textContent = Quotation[whichQuotation][1];
</script>
</div>
</section>

最佳答案

您要查找的是 .querySelector('p') 还是 .querySelector('h4')

document.querySelector('h4').textContent = // Author

试试这个:

<section class="quote">
<div class="content">
<h4></h4>
<p></p>
</div>
</section>
<script>
var Quotation = [];

Quotation[0] = ["example Author 1", "Time is of the essence! Comb your hair."];
Quotation[1] = ["example Author 2", "Sanity is a golden apple with no shoelaces."];
Quotation[2] = ["example Author 3", "Repent! The end is coming, $9.95 at Amazon."];
Quotation[3] = ["example Author 4", "Honesty blurts where deception sneezes."];
Quotation[4] = ["example Author 5", "Pastry satisfies where art is unavailable."];
Quotation[5] = ["example Author 6", "Delete not, lest you, too, be deleted."];
Quotation[6] = ["example Author 7", "O! Youth! What a pain in the backside."];
Quotation[7] = ["example Author 8", "Wishes are like goldfish with propellors."];
Quotation[8] = ["example Author 9", "Love the river's \"beauty\", but live on a hill."];
Quotation[9] = ["example Author 10", "Invention is the mother of too many useless toys."];

var Q = Quotation.length;
var whichQuotation = Math.round(Math.random() * (Q - 1));

document.querySelector('.content > h4').textContent = Quotation[whichQuotation][0];

document.querySelector('.content > p').textContent = Quotation[whichQuotation][1];
</script>

关于javascript - 根据 Javascript 使用 HTML 标签的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com