gpt4 book ai didi

javascript - 如何更改事件执行顺序?

转载 作者:行者123 更新时间:2023-11-28 01:03:55 24 4
gpt4 key购买 nike

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Close window</title>
</head>
<body>
<button id="abc" onclick="temp1()">button</button>

<script type="text/javascript">
function temp1() {
alert("temp1");
};

function temp2() {
alert("temp2");
}

document.getElementById("abc").addEventListener("click", temp2, false);
</script>
</body>
</html>

但我想先显示“temp2”,再显示“temp1”

这可能吗?还是事件执行顺序取决于浏览器,所以我无法更改?

感谢帮助!!

最佳答案

请回顾一下这个:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Close window</title>
</head>

<body>
<!-- here is your first AddEventlistener-->
<button id="abc" onclick="temp1()">button</button>

<script type="text/javascript">
function temp1() {
alert("temp1");
};

function temp2() {
alert("temp2");
}

/*then here is your second AddEventlistener*/
var d = document.getElementById("abc");
d.addEventListener("click", temp2, false);

/*javascript will execute it in order
if you want to change the order. remove first EventListener then register new one after. something like this:
*/
//remove listener
d.onclick = null;
//register new
d.addEventListener('click', temp1);
</script>
</body>

</html>

关于javascript - 如何更改事件执行顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40820399/

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