href激活后如何激活数据删除?

时间:2020-06-13 03:54:05

标签: javascript html jquery bootstrap-4

我有这个链接

<a class='btn btn-danger' href='page.html'>Delete</a>

我想在激活“ href”后激活“数据删除”。我已经尝试过此代码

<a class='btn btn-danger' href='page.html' data-dismiss='modal'>Delete</a>

但是如果我使用“ data-dismiss”,它不会执行“ href”。 (href和数据删除)如何工作?

1 个答案:

答案 0 :(得分:0)

您可以使用button代替a标签,并将css应用于button,使其看起来像link。然后,使用attr向您的元素添加属性。

演示代码

$("button").click(function(){
 $(this).attr("data-dismiss", "modal");//add attribute
 var somecondition=false;
 var href=$(this).attr("href");//get href
 //add some condition if true
 if(somecondition){
 window.location=href;//redirect
 }
 console.log("added")
})
button {
  background: none!important;
  border: none;
  color: #069;
  text-decoration: underline;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='btn btn-danger' href='page.html' >Delete</button>