gpt4 book ai didi

javascript - 使用 JS 单击跨度时更改跨度的 onClick

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:00 25 4
gpt4 key购买 nike

我创建了一个随机生成跨度(以圆圈形式)的程序。我想在点击时更改之前span类的onClick。

我已经尝试将新 span 元素的 id 设置为递归创建的东西 (span.id = "a"+ t; t++;) 或类似的东西。

   <!doctype html>
<link href="https://fonts.googleapis.com/css?
family=Montserrat" rel="stylesheet">
<link rel="icon" type="image/jpg" href="/vNidjVg-
_400x400.jpg"/>
<style>
body{
background-color: #010417;
font-family: 'Montserrat', sans-serif;

}

@keyframes a{
0% {opacity: 0;}
100%{opacity: 1;}
}
button{
background-color: #010417;
border-radius: 10px;
border: 4px solid white;
color: white;
padding: 10px 26px;
font-size: 20px;
}
</style>
<div id="myDIV">
<script>
document.title = prompt("I don't know what to make the
document's title, so what do you want it to be called?");

function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
var but = 2;
var x = 0
function button(){

if(but === 2){
myFunction();
but = 0;

}else{}

}
var t = 0;
var q = -1;
function myFunction() {

var para = document.createElement("SPAN");
para.style.position = "absolute";
x = getRndInteger(0, (window.innerWidth - 60))
para.style.left = x + "px"
var p = getRndInteger(0, (window.innerHeight - 60))
para.style.top = p + "px"
para.style.display = "inline-block;"
para.style.height = "50px"
para.style.width = "50px"
para.style.backgroundColor="red"
para.style.borderRadius = "50px"
para.style.border = "1px solid black"
para.style.animation = "1s a linear"

para.id = "a" + t;

document.getElementById("a" + q).onclick=f
q++
t++;

para.onclick=myFunction

document.getElementById("myDIV").appendChild(para);

}
function f(){}
</script>

</div>
<center>
<button class="1" id="button" onClick="button();">Start</button></center>

我希望它的输出是之前禁用的 span 类(onclick 设置为 f())

最佳答案

首先,您应该在 CSS 中尽可能多地定义元素的样式,而不是在 Javascript 中定义。

其次,在JS中调用onclick时,函数的第一个参数应该是event,代表刚刚出现的点击事件。在 onclick 中,您可以使用 event.toElement 获取被点击的元素,这允许您重新定义 onclick

我认为你正在尝试做的应该是这样的:

var container = document.getElementById("container");

function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}

function spawn() {
var span = document.createElement("SPAN");
span.style.left = getRndInteger(30, (container.offsetWidth - 30)) + "px";
span.style.top = getRndInteger(30, (container.offsetHeight - 30)) + "px";

span.onclick = fA;

container.appendChild(span);
}

function fA(event) {
var span = event.toElement
alert("first click here");
span.onclick = fB;
}

function fB(event) {
alert("second click here");
}
div#container {
width: 300px;
height: 300px;
}

div#container span {
width: 30px;
height: 30px;

position: absolute;

border-radius: 100%;
background-color: red;
}
<button onclick="spawn()">Spawn a span</button>
<div id="container"></div>

关于javascript - 使用 JS 单击跨度时更改跨度的 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55920846/

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