Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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/3/html/83.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一致的HTML标记存在问题_Javascript_Html_Arrays_Tags - Fatal编程技术网

与Javascript一致的HTML标记存在问题

与Javascript一致的HTML标记存在问题,javascript,html,arrays,tags,Javascript,Html,Arrays,Tags,这是我的代码,它可以完美地工作,但只适用于一个报价(稍后将对此进行参考): 但是现在我如何让它们与它们的特定标签相关联: 带有标记的引用和带有标记的作者 特别是报价部分 更新: <section class="quote"> <div class="content"> <h4></h4> <p></p> <script language="JavaScript

这是我的代码,它可以完美地工作,但只适用于一个报价(稍后将对此进行参考):

但是现在我如何让它们与它们的特定标签相关联: 带有
标记的引用和带有
标记的作者

特别是报价部分

更新:

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

/*版权所有2004由CodeScreer.com*/ var报价=[]; 引用语[0]=[“示例作者1”,“时间至关重要!梳理你的头发。”; 引文[1]=“示例作者2”,“理智是没有鞋带的金苹果。”; 引文[2]=[“示例作者3”,“忏悔!末日即将来临,亚马逊9.95美元。”; 引文[3]=[“示例作者4”,“欺骗打喷嚏时诚实脱口而出。”]; 引文[4]=[“示例作者5”,“糕点满足没有艺术的地方。”]; 引用语[5]=[“示例作者6”,“请勿删除,以免您也被删除。”; 引用语[6]=[“示例作者7”,“哦!年轻人!背后多么痛苦。”]; 引文[7]=[“示例作者8”,“愿望就像有推进器的金鱼。”]; 引用语[8]=[“示例作者9”,“热爱河流的“美丽”,但生活在山上。”; 引文[9]=[“示例作者10”,“发明是太多无用玩具之母。”]; var Q=报价单长度; var whichquote=Math.round(Math.random()*(Q-1)); document.querySelector('h4')。textContent=quote[whichquote][0]; document.querySelector('p').textContent=quote[whichquote][1];
.querySelector('p')
还是
.querySelector('h4')
你在找什么

document.querySelector('h4').textContent = // Author
试试这个:


var报价=[]; 引用语[0]=[“示例作者1”,“时间至关重要!梳理你的头发。”; 引文[1]=“示例作者2”,“理智是没有鞋带的金苹果。”; 引文[2]=[“示例作者3”,“忏悔!末日即将来临,亚马逊9.95美元。”; 引文[3]=[“示例作者4”,“欺骗打喷嚏时诚实脱口而出。”]; 引文[4]=[“示例作者5”,“糕点满足没有艺术的地方。”]; 引用语[5]=[“示例作者6”,“请勿删除,以免您也被删除。”; 引用语[6]=[“示例作者7”,“哦!年轻人!背后多么痛苦。”]; 引文[7]=[“示例作者8”,“愿望就像有推进器的金鱼。”]; 引用语[8]=[“示例作者9”,“热爱河流的“美丽”,但生活在山上。”; 引文[9]=[“示例作者10”,“发明是太多无用玩具之母。”]; var Q=报价单长度; var whichquote=Math.round(Math.random()*(Q-1)); document.querySelector('.content>h4')。textContent=quote[whichquote][0]; document.querySelector('.content>p').textContent=quote[whichquote][1];
我想您应该像这样存储和访问报价:

Quotation[0] = "Why must we question pancakes?";
Author[0] = "Sigmund Freud";
var Quotation = [
    {"author": "Sigmund Freud",   "quote": "Why must we question pancakes?"},
    {"author": "Albert Einstein", "quote": "Why are pi squared?"}
]
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
document.write(Quotation[whichQuotation]["author"] + ' said ' + Quotation[whichQuotation]["quote"]);
根据你的评论,我做了一些假设,并对接口的工作方式做了一些自由的考虑。以下是我建议的HTML:

<section class="quote">
    <div class="content">
        <h4>Sigmund Freud</h4>
            <p id="Sigmund Freud">Sigmund Freud quote</p>
        <h4>Albert Einstein</h4>
            <p id="Albert Einstein">Albert Einstein quote</p>
    </div>
    <input type="submit" value="New Quote" onClick="return quotation();" />
</section> 

在jfiddle中(继续单击按钮)。

不清楚您在这里问什么。在第一组代码中,我用“p”标记定义了一个引号,用“h4”标记定义了作者。我想做同样的事,但与脚本相关。我希望随机引用生成器生成一个与作者关联的随机引用,但它们必须用相应的标记“p”和“h4”定义。我希望这更有意义。您打算让第二个代码段尝试如何使第一个代码段成为动态的。这根本不是编写JavaScript来操作DOM的方式。您可能会很清楚,您的第二个代码片段是为了解决问题。太棒了,但我不希望脚本打印出最后一部分。我希望它们被输出,但在它们的标签中。所以:西格蒙德·弗洛伊德将被输出,但在“h4”标签内。报价将在“p”标记内输出。这有意义吗?我想我把事情弄得更复杂了,谢谢。我对Query或JS了解不多,因此我不确定该如何写出。虽然我不希望用户仅通过刷新页面来“单击某个内容”随机生成随机引用。请记住,如果您有多个
,然后,您需要做的是为其分配一个类或id,以便更好地在JavaScript中引用它。更新了示例,向您展示如何将作者链接到引用。试一试这个片段。蒂姆,谢谢你。它似乎在我的编辑器上工作,但我似乎无法获得引用输出。我用你的建议更新了帖子。在“更新:”部分之后。干杯。请尝试删除
language=“JavaScript”
,并将
.querySelector
参数更新为现有参数。看看这是否有效。
var Quotation = [
    {"author": "Sigmund Freud",   "quote": "Why must we question pancakes?"},
    {"author": "Albert Einstein", "quote": "Why are pi squared?"}
]
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
document.write(Quotation[whichQuotation]["author"] + ' said ' + Quotation[whichQuotation]["quote"]);
<section class="quote">
    <div class="content">
        <h4>Sigmund Freud</h4>
            <p id="Sigmund Freud">Sigmund Freud quote</p>
        <h4>Albert Einstein</h4>
            <p id="Albert Einstein">Albert Einstein quote</p>
    </div>
    <input type="submit" value="New Quote" onClick="return quotation();" />
</section> 
function quotation() {
  var Quotation = [
    {"author": "Sigmund Freud",   "quote": "Why must we question pancakes?"}, 
    {"author": "Sigmund Freud",   "quote": "You have mother issues."}, 
    {"author": "Albert Einstein", "quote": "Why are pi squared?"},
    {"author": "Albert Einstein", "quote": "Space is on the chilly side."}
  ];
  var Q = Quotation.length;
  var whichQuotation = Math.round(Math.random() * (Q - 1));
  var el = document.getElementById(Quotation[whichQuotation]["author"]);
  console.log(el);
  el.innerHTML = Quotation[whichQuotation]["quote"];
}