提问者:小点点

href激活后如何激活data-dismist?


我有这个链接

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

我想在“href”激活后激活“data-dismiss”。 我试过这个代码

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

但是如果我使用“data-dismiss”,它不执行“href”。 如何同时(href和data-dismiss)工作?


共1个答案

匿名用户

您可以使用button而不是a标记,并将css应用于按钮,使其看起来像link。 然后,使用attr向元素添加属性。

演示代码:

null

$("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>