如何在按钮标签内添加数据消除?

时间:2017-04-07 06:37:03

标签: jquery html

<button aria-hidden="true">Close</button>

我在html中有一个按钮标签。我想在JQuery的按钮标记中添加<data-dismiss="modal">,使其变得像<button data-dismiss="modal" aria-hidden="true">Close</button>。有没有办法做到这一点?

3 个答案:

答案 0 :(得分:2)

$('button').attr("data-dismiss","modal");  

.attr有2个参数,属性

答案 1 :(得分:0)

您可以使用.attr()执行此操作。首先设置attributeName,然后设置value

&#13;
&#13;
$('button').attr("data-dismiss","modal");
console.log($('button')[0])
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button aria-hidden="true">Close</button>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这里首先没有数据关闭按钮标签,点击打开模态后我附加数据 - 关闭按钮,试试这个

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg open-modal" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Enter Text</p>
          <input id="input_Text" type="text" class="form-control"/>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" >Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>
<script>
$('.open-modal').click(function(){
     $('button').attr("data-dismiss","modal");
});

$('#input_Text').change(function(){
if($('#input_Text').val()!=""){
 $('#myModal').modal('hide');
}
else{
  alert('Give some text');
}

});

</script>
</body>
</html>
&#13;
&#13;
&#13;