javascript - 如何通过单击javascript更改文本

标签 javascript css

对于一个大学元素,我正在创建一个网页来显示倾斜策略短语(由 Brian Eno 和 Peter Schmidt 创建),编写的代码会在刷新页面时拉出一个随机策略,但是我希望该策略能够单击更改(屏幕上的任何位置)而不是必须刷新页面,任何人都可以帮助我吗?

提前谢谢你。

<body>
<div class="container">
<div class="card fade-in">
<p id="randomStrategy"></p>
<script>

// Establish a variable, which pulls from an array.

var selectStrategy = [



    "Abandon desire",
    "Abandon normal instructions",
    "Accept advice",
    "Adding on",
    "A line has two sides",
    "Always the first steps",
    "Ask people to work against their better judgement",
    "Ask your body",
    "Be dirty",
    "Be extravagant",
    "Be less critical",
    "Breathe more deeply",
    "Bridges -build -burn",
    "Change ambiguities to specifics",
    "Change nothing and continue consistently",
    "Change specifics to ambiguities",
    "Consider transitions",
    "Courage!",
    "Cut a vital connection",
    "Decorate, decorate",
    "Destroy nothing; destroy the most important thing",
    "Discard an axiom",
    "Disciplined self-indulgence",
    "Discover your formulas and abandon them",
    "Display your talent",
    "Distort time",
    "Do nothing for as long as possible",
    "Don’t avoid what is easy",
    "Don’t break the silence",
    "Don’t stress one thing more than another",
    "Do something boring",
    "Do something sudden, destructive and unpredictable",
    "Do the last thing first",
    "Do the words need changing?",
    "Emphasise differences",
    "Emphasise the flaws",
    "Faced with a choice, do both (From Dieter Rot.)",
    "Find a safe part and use it as an anchor",
    "Give the game away",
    "Give way to your worst impulse",
    "Go outside. Shut the door.",
    "Go to an extreme, come part way back",
    "How would someone else do it?",
    "How would you have done it?",
    "In total darkness, or in a very large room, very quietly",
    "Is it finished?",
    "Is something missing?",
    "Is the style right?",
    "It is simply a matter or work",
    "Just carry on",
    "Listen to the quiet voice",
    "Look at the order in which you do things",
    "Magnify the most difficult details",
    "Make it more sensual",
    "Make what’s perfect more human",
    "Move towards the unimportant",
    "Not building a wall; making a brick",
    "Once the search has begun, something will be found",
    "Only a part, not the whole",
    "Only one element of each kind",
    "Openly resist change",
    "Pae White’s non-blank graphic metacard",
    "Question the heroic approach",
    "Remember quiet evenings",
    "Remove a restriction",
    "Repetition is a form of change",
    "Retrace your steps",
    "Reverse",
    "Simple Subtraction",
    "Slow preparation, fast execution",
    "State the problem as clearly as possible",
    "Take a break",
    "Take away the important parts",
    "The inconsistency principle",
    "The most easily forgotten thing is the most important",
    "Think -inside the work -outside the work",
    "Tidy up",
    "Try faking it (From Stewart Brand.)",
    "Turn it upside down",
    "Use an old idea",
    "Use cliches",
    "Use filters",
    "Use something nearby as a model",
    "Use ‘unqualified’ people",
    "Use your own ideas",
    "Voice your suspicions",
    "Water",
    "What context would look right?",
    "What is the simplest solution?",
    "What mistakes did you make last time?",
    "What to increase? What to reduce? What to maintain?",
    "What were you really thinking about just now?",
    "What wouldn’t you do?",
    "What would your closest friend do?",
    "When is it for?",
    "Where is the edge?",
    "Which parts can be grouped?",
    "Work at a different speed",
    "Would anyone want it?",
    "Your mistake was a hidden intention",
    "Use fewer notes",
    "Use filters",
    "Use ‘unqualified’ people",
    "Water",
    "What are you really thinking about just now? Incorporate",
    "What is the reality of the situation?",
    "What mistakes did you make last time?",
    "What would your closest friend do?",
    "What wouldn’t you do?",
    "Work at a different speed",
    "You are an engineer",
    "You can only make one dot at a time",
    "You don’t have to be ashamed of using your own ideas",
    // The following is where the randomness magic happens. 
    ];
var pickAStrategy = function () {
var randomStrategy = selectStrategy[Math.floor(Math.random() * 
selectStrategy.length)];
return randomStrategy;
    };
    // This writes the strategy to the page.

document.getElementById("randomStrategy").innerHTML = pickAStrategy();
    </script>       
</div> <!-- /card -->
</div> <!-- /container -->
</body>
</html>

最佳答案

你想要这样的东西吗?

var selectStrategy = [
    "Abandon desire",
    "Abandon normal instructions",
    "Accept advice",
    "Adding on",
    "A line has two sides",
    "Always the first steps",
    "Ask people to work against their better judgement",
    "Ask your body",
    "Be dirty",
    "Be extravagant",
    "Be less critical",
    "Breathe more deeply",
    "Bridges -build -burn",
    "Change ambiguities to specifics",
    "Change nothing and continue consistently",
    "Change specifics to ambiguities",
    "Consider transitions",
    "Courage!",
    "Cut a vital connection",
    "Decorate, decorate",
    "Destroy nothing; destroy the most important thing",
    "Discard an axiom",
    "Disciplined self-indulgence",
    "Discover your formulas and abandon them",
    "Display your talent",
    "Distort time",
    "Do nothing for as long as possible",
    "Don’t avoid what is easy",
    "Don’t break the silence",
    "Don’t stress one thing more than another",
    "Do something boring",
    "Do something sudden, destructive and unpredictable",
    "Do the last thing first",
    "Do the words need changing?",
    "Emphasise differences",
    "Emphasise the flaws",
    "Faced with a choice, do both (From Dieter Rot.)",
    "Find a safe part and use it as an anchor",
    "Give the game away",
    "Give way to your worst impulse",
    "Go outside. Shut the door.",
    "Go to an extreme, come part way back",
    "How would someone else do it?",
    "How would you have done it?",
    "In total darkness, or in a very large room, very quietly",
    "Is it finished?",
    "Is something missing?",
    "Is the style right?",
    "It is simply a matter or work",
    "Just carry on",
    "Listen to the quiet voice",
    "Look at the order in which you do things",
    "Magnify the most difficult details",
    "Make it more sensual",
    "Make what’s perfect more human",
    "Move towards the unimportant",
    "Not building a wall; making a brick",
    "Once the search has begun, something will be found",
    "Only a part, not the whole",
    "Only one element of each kind",
    "Openly resist change",
    "Pae White’s non-blank graphic metacard",
    "Question the heroic approach",
    "Remember quiet evenings",
    "Remove a restriction",
    "Repetition is a form of change",
    "Retrace your steps",
    "Reverse",
    "Simple Subtraction",
    "Slow preparation, fast execution",
    "State the problem as clearly as possible",
    "Take a break",
    "Take away the important parts",
    "The inconsistency principle",
    "The most easily forgotten thing is the most important",
    "Think -inside the work -outside the work",
    "Tidy up",
    "Try faking it (From Stewart Brand.)",
    "Turn it upside down",
    "Use an old idea",
    "Use cliches",
    "Use filters",
    "Use something nearby as a model",
    "Use ‘unqualified’ people",
    "Use your own ideas",
    "Voice your suspicions",
    "Water",
    "What context would look right?",
    "What is the simplest solution?",
    "What mistakes did you make last time?",
    "What to increase? What to reduce? What to maintain?",
    "What were you really thinking about just now?",
    "What wouldn’t you do?",
    "What would your closest friend do?",
    "When is it for?",
    "Where is the edge?",
    "Which parts can be grouped?",
    "Work at a different speed",
    "Would anyone want it?",
    "Your mistake was a hidden intention",
    "Use fewer notes",
    "Use filters",
    "Use ‘unqualified’ people",
    "Water",
    "What are you really thinking about just now? Incorporate",
    "What is the reality of the situation?",
    "What mistakes did you make last time?",
    "What would your closest friend do?",
    "What wouldn’t you do?",
    "Work at a different speed",
    "You are an engineer",
    "You can only make one dot at a time",
    "You don’t have to be ashamed of using your own ideas",
];

var pickAStrategy = function () {
  var randomStrategy = selectStrategy[Math.floor(Math.random() * selectStrategy.length)];
  document.getElementById('randomStrategy').innerHTML = randomStrategy;
};

pickAStrategy();
document.addEventListener('click', pickAStrategy, true);
<div class="container">
  <div class="card fade-in">
    <p id="randomStrategy"></p>  
  </div>
</div>

关于javascript - 如何通过单击javascript更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668349/

相关文章:

javascript - 使用两个 jQuery 函数同时更改单个元素的不透明度?

javascript - CSS3 transition 和 transform 在类移除时效果不佳

css - 溢出:滚动导致图像被截断

javascript - CSS 无法对 Knockout 绘制的表格应用 dark-stripe 和 light-stripe

javascript - Gmail JS API - OAuth2 错误 invalid_scope

css - 在phpstorm中配置css自动补全

jquery - 水平 Accordion 切换

javascript - CasperJS 中的静默错误

javascript - 在点击事件中,第一次点击时触发一次代码,其余代码在所有点击时运行

javascript - 计算 4x4 矩阵的行列式 - (RayTracer 挑战) - Javascript