简体   繁体   English

如何在HTML中加载JavaScript onclick?

[英]How do I load a JavaScript onclick in HTML?

This question is pertaining to my music discovery tool that I have created. 这个问题与我创建的音乐发现工具有关。 DREAMFM 梦幻FM

Within a div element, I have a JavaScript that randomly selects some HTML from an array. 在div元素中,我有一个JavaScript,可从数组中随机选择一些HTML。 The randomly selected HTML is then written to my document, the index. 然后将随机选择的HTML写入我的文档(索引)。 Each number in the array is a different song that is written to the HTML. 数组中的每个数字都是写入HTML的不同歌曲。 The only way to get to the next new song, is to refresh the page currently. 转到下一首新歌的唯一方法是当前刷新页面。 I would like to load the JavaScript onclick of a button instead of having to reload the page to get to the next song. 我想加载按钮的JavaScript onclick,而不必重新加载页面以获取下一首歌曲。

Currently I am using this method to reload the external JavaScript: 目前,我正在使用此方法重新加载外部JavaScript:

<button id="nextbtn" type="button" onclick="shuffle()" autofocus>></button>

<script>
function shuffle() {
location.reload(true); 
}
</script>

I would like to load only the JavaScript without reloading the entire page. 我只想加载JavaScript,而不重新加载整个页面。

Do not write your code / Do not implement your JS file inline with html. 不要编写代码/不要用html内联实现JS文件。 Wrap your code inside a function and call it as you need. 将代码包装在一个函数中,然后根据需要调用它。 Onload or Onclick: Onload或Onclick:

function displayRandomSong(){
// put all of Your current code here if there is no function and
// every thing is pure javascript codes. I think this meets your need.

// However if your JS file is a complex of many functions, There is always 
// a function Which is triggered before others. Call that function here
// For example:
// startRandomization()
 }

So the html would be this: 因此,HTML将是这样的:

<a onclick="displayRandomSong()">inline refresh</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用onclick使用javascript将视频加载到HTML5页面 - How do I load a video into a HTML5 page with javascript using onclick 使用onclick事件时如何加载html内容? - How do I load html content in when using onclick event? Javascript入门:如何将预加载器添加到HTML onClick? - Javascript Beginner: How do I add preloader to HTML onClick? 如何在 javascript 中使用 html 中的元素进行 onclick 事件? - How do I use elements from html in javascript for an onclick event? HTML / JavaScript的新手,如何使用onclick更改JavaScript的背景颜色 - Very new to HTML/JavaScript, how do I use onclick to change background color with JavaScript 如何在页面加载时预加载Javascript编写的HTML? - How do I preload HTML that is written by a Javascript on page load? 如何在HTML标头中加载可选的JavaScript - How do I load a selectable javascript in html header 如何在 html 元素中加载外部 javascript function? - How do I load an external javascript function in an html element? 如何使用 JavaScript 在 div 中加载 HTML 页面? - How do I load an HTML page in a div using JavaScript? 当我将 JavaScript function 导入到我的 Z4C4DBAD5FCA2E78AAA47F 文件中时,如何修复无法工作的 onclick - How do I fix an onclick that isn't working when I import a JavaScript function into my HTML file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM