【问题标题】:show/hide alertbox with data-attribute in twitter bootstrap在 twitter bootstrap 中显示/隐藏带有数据属性的警报框
【发布时间】:2013-03-31 19:04:48
【问题描述】:

我想在 twitter bootstrap 中使用数据属性显示隐藏警报框,这是因为我可以在一页中使用多个警报框...

目前我有这个,

<a href="#" data-box="myAlert">Hint</a> //data-box is the id of the box

<div id="myAlert" class="alert alert-info">
    <a class="close" data-dismiss="alert" href="#">&times;</a>
    <p>Alert Me !!!!</p>
</div>

【问题讨论】:

  • 你能解释一下它为什么不适合你吗?
  • 我没有这个 jquery...

标签: jquery html twitter-bootstrap custom-data-attribute


【解决方案1】:

试试

$(function(){

  $('[data-box]').click(function(){
    var $this = $(this);
    $('#' + $this.data('box')).show()
    return false;
  });

  $(document).on('click', '.alert .close', function(){
    $(this).closest('.alert').hide()
  });

});

演示:Plunker

更新
使用锚标签显示/隐藏

<a href="#" data-box="myAlert">Hint</div>

<div id="myAlert" class="alert alert-info hide">
    <p>Alert Me !!!!</p>
</div>  

$(function(){

  $('[data-box]').click(function(){
    var $this = $(this);
    $('#' + $this.data('box')).toggle()
    return false;
  });

});

演示:Fiddle

【讨论】:

  • 这是因为使用了bootstrap提供的close按钮,点击时会删除警告框
  • @SaurabhLP 如果您希望它在单击的链接将上述代码中的 .show() 更改为 .toggle() 时再次隐藏
  • @SaurabhLP 编写你自己的关闭方法,它只是隐藏警报而不是删除它
  • @ArunPJohny 如果我删除关闭按钮并制作“提示”锚点以显示/隐藏...
  • @SaurabhLP 会更好
猜你喜欢
  • 1970-01-01
  • 2021-04-12
  • 2014-02-28
  • 2012-12-07
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
相关资源
最近更新 更多