简体   繁体   English

使用单一方法显示或隐藏div的按钮

[英]Buttons to show or hide divs using single method

I have the following snippets: My HTML Page which uses 4 buttons to show/hide four divs, each will have their own content. 我有以下代码片段:我的HTML页面,它使用4个按钮来显示/隐藏四个div,每个都有自己的内容。

 var otherDivs = ["addCustomer", "searchCustomer", "addItem", "searchItem"]; function show(target) { var index = otherDivs.indexOf(target); for (i = 0; i < otherDivs.length; i++) { if (i != index) { $(document.getElementById(otherDivs[i].toString())).hide(); } else { $(document.getElementById(otherDivs[i].toString())).show(); } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid" style="padding-top: 10px;"> <div class="row" name="tabs"> <button class="redButton" onclick="show('addCustomer')">Add Customer</button> <button class="redButton" onclick="show('searchCustomer')">Search Customers</button> <button class="redButton" onclick="show('addItem')">Add Item</button> <button class="redButton" onclick="show('searchItem')">Search Item</button> </div> <div class="row trigger" name="searchCustomer" id="searchCustomer"> ... </div> <div class="row trigger" name="addCustomer" id="addCustomer"> ... </div> <div class="row trigger" name="addItem" id="addItem"> ... </div> <div class="row trigger" name="searchItem" id="searchItem"> ... </div> </div> 

The problem is, only the fist button and div (addCustomer) works correctly. 问题是,只有拳头按钮和div(addCustomer)可以正常工作。 I have tried split methods but I would prefer to keep the single method. 我尝试了分割方法,但我希望保留单一方法。 Any help is appreciated. 任何帮助表示赞赏。

Use jQuery fully when you have it. 拥有jQuery时,请充分使用它。

Although with your HTML and inline script this would work: 尽管使用您的HTML和内联脚本可以运行:

function show(target) {
  $('.trigger').hide();
  $("#"+ target).show();
}

As could this (but it would not work as a radio button more like a checkbox): 可以这样做(但它不能像单选按钮那样用作单选按钮):

function show(target) {
  $("#"+ target).toggle();
}

I strongly suggest you use unobtrusive code and move the script out of the HTML 我强烈建议您使用简洁的代码并将脚本移出HTML

 $(function() { $(".redButton").on("click",function() { // any button $(".trigger").hide(); // hide the divs $("#"+$(this).data("id")).show(); // show the relevant div }); }); 
 .trigger { display:none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid" style="padding-top: 10px;"> <div class="row" name="tabs"> <button type="button" class="redButton" data-id="addCustomer">Add Customer</button> <button type="button" class="redButton" data-id="searchCustomer">Search Customers</button> <button type="button" class="redButton" data-id="addItem">Add Item</button> <button type="button" class="redButton" data-id="searchItem">Search Item</button> </div> <div class="row trigger" name="searchCustomer" id="searchCustomer"> Search Customer </div> <div class="row trigger" name="addCustomer" id="addCustomer"> Add Customer </div> <div class="row trigger" name="addItem" id="addItem"> Add Item </div> <div class="row trigger" name="searchItem" id="searchItem"> Search Item </div> </div> 

Actually your code working properly but the thing is that you cannot specfiy which div is opened now because all divs have the same content, so when you add numbers or a different content you can know which one is opened now 实际上您的代码可以正常工作,但问题是您无法指定现在打开哪个div因为所有divs都具有相同的内容,因此当您添加数字或其他内容时,您可以知道现在打开了哪个div

 var otherDivs = ["addCustomer", "searchCustomer", "addItem", "searchItem"]; function show(target) { var index = otherDivs.indexOf(target); for (i = 0; i < otherDivs.length; i++) { if (i != index) { $(document.getElementById(otherDivs[i].toString())).hide(); } else { $(document.getElementById(otherDivs[i].toString())).show(); } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid" style="padding-top: 10px;"> <div class="row" name="tabs"> <button class="redButton" onclick="show('addCustomer')">Add Customer</button> <button class="redButton" onclick="show('searchCustomer')">Search Customers</button> <button class="redButton" onclick="show('addItem')">Add Item</button> <button class="redButton" onclick="show('searchItem')">Search Item</button> </div> <div class="row trigger" name="searchCustomer" id="searchCustomer"> .11.. </div> <div class="row trigger" name="addCustomer" id="addCustomer"> .22.. </div> <div class="row trigger" name="addItem" id="addItem"> .33.. </div> <div class="row trigger" name="searchItem" id="searchItem"> .44.. </div> </div> 

Simpy use the JQuery toggle function like so Simpy像这样使用JQuery toggle功能

function show(target) {
  $('#'+target).toggle();
}

You could replace your entire JS in the snippet to just these LOC. 您可以将代码段中的整个JS替换为这些LOC。 HTML remains the same. HTML保持不变。

 function show(target) { $('#'+target).toggle(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid" style="padding-top: 10px;"> <div class="row" name="tabs"> <button class="redButton" onclick="show('addCustomer')">Add Customer</button> <button class="redButton" onclick="show('searchCustomer')">Search Customers</button> <button class="redButton" onclick="show('addItem')">Add Item</button> <button class="redButton" onclick="show('searchItem')">Search Item</button> </div> <div class="row trigger" name="searchCustomer" id="searchCustomer"> ... </div> <div class="row trigger" name="addCustomer" id="addCustomer"> ... </div> <div class="row trigger" name="addItem" id="addItem"> ... </div> <div class="row trigger" name="searchItem" id="searchItem"> ... </div> </div> 

Updated answer 更新的答案

  function show(target) { $("#Display").html( $('#' + target).html()); } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid" style="padding-top: 10px;"> <div class="row" name="tabs"> <button class="redButton" onclick="show('addCustomer')">Add Customer</button> <button class="redButton" onclick="show('searchCustomer')">Search Customers</button> <button class="redButton" onclick="show('addItem')">Add Item</button> <button class="redButton" onclick="show('searchItem')">Search Item</button> </div> <div class="row trigger" name="searchCustomer" id="searchCustomer" hidden> Searching Customers </div> <div class="row trigger" name="addCustomer" id="addCustomer" hidden> Customers added </div> <div class="row trigger" name="addItem" id="addItem" hidden> Items added </div> <div class="row trigger" name="searchItem" id="searchItem" hidden> Searching Items </div> <div id="Display"> </div> </div> 

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

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