html - Bootstrap 按钮组显示/隐藏 Div

标签 html twitter-bootstrap

我有一个 Bootstrap 按钮组,其中有 2 个按钮(JSON 和 XML),加载页面时 JSON 处于事件状态。按 XML 按钮将自动将焦点更改到它。

现在,我想为这些按钮附加一些行为,以便单击 XML 按钮隐藏 error-json pre 元素并显示 error-xml pre元素。

是否有一种Bootstrap-native方法来完成此任务?

<div class="btn-group">
    <button class="btn btn-default" autofocus="true">JSON</button>
    <button class="btn btn-default">XML</button>
</div>

<pre id="error-json" class="hidden"><code class="language-json">{ 
  "ErrorCode": STATUS_CODE,
  "Description": ERROR_MSG
}</code></pre>

<pre id="error-xml"><code class="language-xml">&lt;error&gt;
  &lt;ErrorCode&gt;STATUS_CODE&lt;/ErrorCode&gt;
  &lt;Description&gt;ERROR_MSG&lt;/Description&gt;
&lt;/error&gt;
</code></pre>

最佳答案

$("#error-xml").hide();
$(".btn").click(function () {
	var id = $(this).attr("id");
  if(id == "json"){
  $(this).attr("autofocus",true).siblings().attr("autofocus",false);
  	$("#error-xml").hide();
  	$("#error-json").show();
  }else{
  $(this).attr("autofocus",true).siblings().attr("autofocus",false);
  	$("#error-json").hide();
  	$("#error-xml").show();
  }
});
:focus{
  outline: 1px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<div class="btn-group">
    <button class="btn btn-default" id="json" autofocus="autofocus">JSON</button>
    <button class="btn btn-default" id="xml">XML</button>
</div>

<pre id="error-json" class="hidden"><code class="language-json">{ 
  "ErrorCode": STATUS_CODE,
  "Description": ERROR_MSG
}</code></pre>

<pre id="error-xml"><code class="language-xml">&lt;error&gt;
  &lt;ErrorCode&gt;STATUS_CODE&lt;/ErrorCode&gt;
  &lt;Description&gt;ERROR_MSG&lt;/Description&gt;
&lt;/error&gt;
</code></pre>

关于html - Bootstrap 按钮组显示/隐藏 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43147775/

相关文章:

javascript - 将具有不同 id 的元素拖放到它们相应的放置区

html - 这两对标签的行为有何不同(参见)?

javascript - Bootstrap 导航栏折叠问题

javascript - 如何使用编辑窗口编辑表格中的行

html - 当悬停时将工具提示所在的元素设置为较低的值时,如何使工具提示的不透明度为 1

twitter-bootstrap - Bootstrap v4 表单,同一行有 3 个输入 + 按钮

html - 根据设备宽度更改视口(viewport)标签?

javascript - 如何在 HTML 中获取元素的形状/多边形/边界路径/框架?

html - 如何设计 Material 工具提示 Angular 7

css - 自举网格不均匀列