简体   繁体   English

单击锚点时显示引导警报消息框

[英]Displaying bootstrap alert message box when an anchor is clicked

I have an anchor element which removes items when it is clicked. 我有一个锚元素,单击该元素会删除项目。 I am trying to show bootstrap danger alert message box (NOT alert window) when the anchor tag is clicked. 单击锚标记时,我试图显示引导程序危险警报消息框(“非警报”窗口)。 i am trying to achieve this without wrapping the anchor in div. 我试图做到这一点而无需将锚包裹在div中。 any help is appreciated. 任何帮助表示赞赏。

 $(document).ready(function(){ $('.remove-item').click(function(){ $('.alert').show() setTimeout(function(){ $(".remove-item").hide(); }, 2000); }); }); 
 .alert{ display: none; } 
 <a href="#" class="btn btn-danger remove-item" data-dismiss="alert" data-code="<?php echo $product_code; ?>"><i class="glyphicon glyphicon-trash"></i></a> 

Here is a little solution you can rely on. 这是您可以依靠的一些解决方案。 It is in vanillia JS. 它在vanillia JS中。

HTML HTML

<button class="anchor">test 1</button>
<button class="anchor">test 2</button>
<button class="anchor">test 3</button>
<button class="anchor">test 4</button>

<div id="alert">
<strong>Are you sure?</strong>
<button id="alert-ok">ok</button>
</div>

CSS CSS

#alert {
  display: none;
}

JS JS

var buttons = document.getElementsByClassName('anchor');

var alertBox = document.getElementById("alert");

var alertBoxOk = document.getElementById("alert-ok");


Array.prototype.forEach.call(buttons, function (btn) {
    btn.addEventListener("click", function (event) {
  var toRemove = event.target;
  alertBox.style.display = "block";
  console.log(toRemove);
  alertBoxOk.addEventListener("click", function() {
    toRemove.remove();
    alertBox.style.display = "none";
  });
  })
});

function deleteAnchor() {
    this.remove();
}

And the fiddle: fiddle 小提琴: 小提琴

There is no problem with the classes your are using for the Bootstrap alert and no problem with the click event either, but UNEXPECTED END OF INPUT means that there is a class or a function that is not closed. 用于Bootstrap警报的类没有问题,单击事件也没有问题,但是UNEXPECTED END OF INPUT表示存在未关闭的类或函数。 In your case, you declared two functions, but only one is closed... : 在您的情况下,您声明了两个函数,但是只有一个关闭了...:

$(document).ready(function(){
  $('.remove-item').click(function(){
  $('.alert').show()
  setTimeout(function(){
     $(".remove-item").hide(); 
  }, 2000);
});

Uncaught ReferenceError: $ is not defined means that the JQuery library is not installed... Do you have the required script tag ? Uncaught ReferenceError:未定义$表示未安装JQuery库...是否具有必需的脚本标记? Something like : 就像是 :

<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM