将点击的 ID 分配给变量

时间:2021-07-10 10:52:45

标签: javascript html

想知道如何在点击时为变量分配 ID。

然后,我想用这个变量设置为标题。

但是,这段代码会很长。因此,当我单击一个元素时,我想将其 ID 设置为一个变量,然后将其用作标题。我该怎么做?

这就是我得到这么久的:

function myFunction1() {
(async () => {

const ipAPI = '//api.ipify.org?format=json'

const inputValue = fetch(ipAPI)
.then(response => response.json())
.then(data => data.ip)

const { value: elemento } = await Swal.fire({
title: 'H',
text: '¿Cuál es el nombre de este elemento?',
input: 'text',
inputPlaceholder: 'Nombre',
showCancelButton: true,
inputValidator: (value) => {
 if (!value) {
   return 'You need to write something!'
 }
}
})

if (elemento === 'Hidrógeno') {
Swal.fire({
 title: 'Correcto!',
 text: 'Has acertado la pregunta.',
 icon: 'success',
 confirmButtonText: 'Siguiente'
})
}
 
else {
Swal.fire({
 title: 'Incorrecto...',
 text: "La respuesta correcta era Hidrógeno!",
 icon: 'error',
 confirmButtonText: 'Volver a intentar'
})
}

})(); }```

0 个答案:

没有答案