gpt4 book ai didi

javascript - 不使用 jQuery 重写随机/按钮 Javascript

转载 作者:行者123 更新时间:2023-12-01 02:42:50 25 4
gpt4 key购买 nike

我有这个脚本。在这种情况下它工作得很好,但它似乎与我正在处理的站点中的其他脚本发生冲突。我怎样才能重写它而不需要 jQuery/OnLoad?

这就是它的作用:每次单击该按钮时,该按钮都会在新窗口中打开一个来自数组的随机链接。

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>

<title>TestBase</title>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
var links = [
"http://vetteletters.nl",
"http://todont.co",
"http://planetx.nl/titles"
];
$("#rnd_link").click(function(){
window.open(links[Math.floor((Math.random()*3))]);
});
});//]]>

</script>


</head>

<body>

<button id="rnd_link">Random</button>

</body>

</html>

最佳答案

window.onload = function(){
var links = [
"http://vetteletters.nl",
"http://todont.co",
"http://planetx.nl/titles"
];

var btn = document.getElementById('rnd_link');
btn.onclick = function(){
window.open(links[Math.floor((Math.random()*3))]);
}
}//]]>

应该可以。

关于javascript - 不使用 jQuery 重写随机/按钮 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47441550/

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