如何使用javascript触发HTML中的按钮?
我尝试使用< script>< / script> HTML文件,它工作得很好,但是当我单击HTML中的按钮时,我想使用JavaScript文件触发.
这是代码:
HTML< head>< / head>中的JAVASCRIPT标签:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js">
<script src="jsFiles/index.js"></script>
HTML:
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Message</a>
<a class="dropdown-item" href="#">Settings</a>
<a class="dropdown-item" id="a_id">Log Out</a>
</div>
JAVASCRIPT文件:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
$(document).ready(function(){
$("#a_id").click(function(){
swal({title:'logout',text:'Do you want to logout this Account?',icon:'warning',buttons: true,dangerMode: true
})
.then((willOUT) => {
if (willOUT) {
window.location.href = 'page-logout.PHP',{
icon: 'success',}
}
});
});
});
</script>
最佳答案
您应该删除所有不是javascript代码的内容,因此文件index.js应该如下所示:
原文链接:https://www.f2er.com/html/530453.html$(document).ready(function() {
$("#a_id").click(function() {
swal({
title: 'logout',text: 'Do you want to logout this Account?',icon: 'warning',dangerMode: true
})
.then((willOUT) => {
if (willOUT) {
window.location.href = 'page-logout.PHP',{
icon: 'success',}
}
});
});
});