有没有办法将 HTML hrefs 实现到 JS 数组中,并在调用时打开相应的 link?
Is there a way to implement HTML hrefs into a JS array, and when called on opens the respective link?
我有一个 JavaScript 数组,我想将 HTML link 放入该数组中。我不知道是否有某种方法可以解决这个问题。另外,如果可能的话,当用户单击按钮并选择 link 时,有没有办法让 link 在新选项卡中打开(我知道 HTML 它目标=_空白)。我一如既往地感谢任何帮助。
JavaScript:
var i = Math.floor(Math.random() * 10) +1;
function randomPageWithJS() {
const arrayJS = [link1, link2, link3, link4];
document.getElementById('randomPageWithJS').value = arrayJS[i];
}
randomPageWithJS();
HTML:
<form>
Random Song:<input type="text" id="randomPageWithJS" name="randomPage"/>
<input type="button" value="RandomPage" onclick="randomPageWithJS()" width="1000"/>
</form>
使用window.open
window.open(arrayJS[i], '_blank');
示例:
function randomPage() {
const links = ['https://google.com', 'https://whosebug.com', '
window.open(links[Math.floor(Math.random()*links.length)], '_blank');
}
<input type="button" value="Random Page" onclick="randomPage()">
我有一个 JavaScript 数组,我想将 HTML link 放入该数组中。我不知道是否有某种方法可以解决这个问题。另外,如果可能的话,当用户单击按钮并选择 link 时,有没有办法让 link 在新选项卡中打开(我知道 HTML 它目标=_空白)。我一如既往地感谢任何帮助。
JavaScript:
var i = Math.floor(Math.random() * 10) +1;
function randomPageWithJS() {
const arrayJS = [link1, link2, link3, link4];
document.getElementById('randomPageWithJS').value = arrayJS[i];
}
randomPageWithJS();
HTML:
<form>
Random Song:<input type="text" id="randomPageWithJS" name="randomPage"/>
<input type="button" value="RandomPage" onclick="randomPageWithJS()" width="1000"/>
</form>
使用window.open
window.open(arrayJS[i], '_blank');
示例:
function randomPage() {
const links = ['https://google.com', 'https://whosebug.com', '
window.open(links[Math.floor(Math.random()*links.length)], '_blank');
}
<input type="button" value="Random Page" onclick="randomPage()">