展示或隐藏多个表单使用JavaScript。

huangapple go评论72阅读模式
英文:

Show or hide multiple forms with javascript

问题

我正在尝试创建一段JavaScript代码,该代码将仅隐藏和显示页面上与其id属性对应的每个表单。因此,想象一下在页面上有4个可见的按钮,表单元素默认情况下是隐藏的,一旦单击与表单匹配的id的单个按钮,它将仅切换该表单。以下是我尝试的代码:

function _(x) {
  return document.getElementById(x);
}

const catBtn = _("catBtn");
const blogBtn = _("blogBtn");
const videoBtn = _("VideoBtn");
const prodBtn = _("prodBtn");

const catForm = _("catForm");
const blogForm = _("blogForm");
const videoForm = _("VideoForm");
const prodForm = _("prodForm");

document.addEventListener('click', function (e) {
  if (e.target.id === catBtn) {
    // catForm.classList.toggle("showHide");
    console.log("你点击了类别按钮");
  } else if (e.target.id === blogBtn) {
    // blogForm.classList.toggle("showHide");
    console.log("你点击了博客按钮");
  } else if (e.target.id === videoBtn) {
    // videoForm.classList.toggle("showHide");
    console.log("你点击了视频按钮");
  } else if (e.target.id === prodForm) {
    // prodForm.classList.toggle("showHide");
    console.log("你点击了产品按钮");
  }
})
<div class="new__btn">
  <button id="catBtn">新类别</button>
</div>
<div class="new__btn">
  <button id="blogBtn">新博客</button>
</div>
<div class="new__btn">
  <button id="videoBtn">新视频</button>
</div>
<div class="new__btn">
  <button id="prodBtn">新产品</button>
</div>

<div id="catForm">
  隐藏或显示此类别的div
  <div class="panel">
    <div class="panel__head">面板头部</div>
    <div class="panel__body">面板主体</div>
    <div class="panel__footer">面板底部</div>
  </div>
</div>

<div id="blogForm">
  隐藏或显示博客的div
  <div class="panel">
    <div class="panel__head">面板头部</div>
    <div class="panel__body">面板主体</div>
    <div class="panel__footer">面板底部</div>
  </div>
</div>

<div>
  <div id="videoForm">
    隐藏或显示视频的div
    <div class="panel">
      <div class="panel__head">面板头部</div>
      <div class="panel__body">面板主体</div>
      <div class="panel__footer">面板底部</div>
    </div>
  </div>

  <div id="prodForm">
    隐藏或显示产品的div
    <div class="panel">
      <div class="panel__head">面板头部</div>
      <div class="panel__body">面板主体</div>
      <div class="panel__footer">面板底部</div>
    </div>
  </div>
</div>

希望这对你有帮助。如果你有任何问题或需要进一步的帮助,请随时告诉我。

英文:

I am trying to create a piece of javascript code that will only hide and show each form on the page that corresponds with its id property. So imagine having 4 buttons visible on the page and the form elements are hidden by default, once a single button with its matching id to the form is clicked it will only toggle that form. Here is the code i have tried

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function _(x) {
return document.getElementById(x);
}
const catBtn = _(&quot;catBtn&quot;);
const blogBtn = _(&quot;blogBtn&quot;);
const videoBtn = _(&quot;VideoBtn&quot;);
const prodBtn = _(&quot;prodBtn&quot;);
const catForm = _(&quot;catForm&quot;);
const blogForm = _(&quot;blogForm&quot;);
const videoForm = _(&quot;VideoForm&quot;);
const prodForm = _(&quot;prodForm&quot;);
document.addEventListener(&#39;click&#39;, function(e) {
if (e.target.id === catBtn) {
// catForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the cat btn&quot;);
} else if (e.target.id === blogBtn) {
// blogForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the blog btn&quot;);
} else if (e.target.id === videoBtn) {
// videoForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the video btn&quot;);
} else if (e.target.id === prodForm) {
// prodForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the product btn&quot;);
}
})

<!-- language: lang-html -->

&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;catBtn&quot;&gt;New Category&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;blogBtn&quot;&gt;New Blog&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;videoBtn&quot;&gt;New Video&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;prodBtn&quot;&gt;New Product&lt;/button&gt;
&lt;/div&gt;
&lt;div id=&quot;catForm&quot;&gt;
Hide or show this div for the categories
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;blogForm&quot;&gt;
Hide or show this div for the blogs
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div id=&quot;videoForm&quot;&gt;
Hide or show this div for the videos
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;prodForm&quot;&gt;
Hide or show this div for the products
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您要的代码的翻译部分:

(function(){
    // 查找所有具有 data-target-form 属性的按钮
    const buttons = document.body.querySelectorAll('button[data-target-form]')

    // 遍历每个按钮
    buttons.forEach((btn) => {
        // 为每个按钮添加事件处理程序
        btn.onclick = (e) => {
            // 获取目标表单名称
            let formName = e.target.dataset.targetForm;
            // 隐藏不是所需表单的表单
            document.body.querySelectorAll(`div[data-form-id]:not([data-form-id="${formName}"])`).forEach((form) => form.style.display = 'none');
            // 显示所需的表单
            document.body.querySelector(`div[data-form-id="${formName}"]`).style.display = 'block';
        }
    });

})()
/* 首先隐藏所有表单,直到单击按钮 */
div[data-form-id]{
  display: none;
}

/* 将所有按钮放在一排 */
.new__btn {
  display: inline-block;
}
<div class="new__btn">
  <button id="catBtn" data-target-form="categories">新类别</button>
</div>
<div class="new__btn">
  <button id="blogBtn" data-target-form="blogs">新博客</button>
</div>
<div class="new__btn">
  <button id="videoBtn" data-target-form="videos">新视频</button>
</div>
<div class="new__btn">
  <button id="prodBtn" data-target-form="products">新产品</button>
</div>

<div id="catForm" data-form-id="categories">
  隐藏或显示此类别的 div
  <div class="panel">
    <div class="panel__head">面板标题</div>
    <div class="panel__body">面板正文</div>
    <div class="panel__footer">面板页脚</div>
  </div>
</div>

<div data-form-id="blogs">
  隐藏或显示此博客的 div
  <div class="panel">
    <div class="panel__head">面板标题</div>
    <div class="panel__body">面板正文</div>
    <div class="panel__footer">面板页脚</div>
  </div>
</div>

<div>
  <div data-form-id="videos">
    隐藏或显示此视频的 div
    <div class="panel">
      <div class="panel__head">面板标题</div>
      <div class="panel__body">面板正文</div>
      <div class="panel__footer">面板页脚</div>
    </div>
  </div>

  <div data-form-id="products">
    隐藏或显示此产品的 div
    <div class="panel">
      <div class="panel__head">面板标题</div>
      <div class="panel__body">面板正文</div>
      <div class="panel__footer">面板页脚</div>
    </div>
  </div>
</div>
英文:

I would take an entirely different approach with this problem. What I suggest is tagging your buttons and forms using data attribues and then dynamically showing and hiding the forms based on what button was clicked. This will allow you to easily add new forms.

Here is an implementation of my approach that uses querySelectory and data attributes. If you have any questions please feel free to ask away and I'll do my best to elaborate on my solution for you.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

(function(){
// find all the buttons with a data-target-form attribute 
const buttons = document.body.querySelectorAll(&#39;button[data-target-form]&#39;)
// loop over each button
buttons.forEach((btn) =&gt; {
// wire up the event handler for each button
btn.onclick = (e) =&gt; {
// get the target form name
let formName = e.target.dataset.targetForm;
// hide forms that aren&#39;t the desired one
document.body.querySelectorAll(`div[data-form-id]:not([data-form-id=&quot;${formName}&quot;])`).forEach((form) =&gt; form.style.display = &#39;none&#39;);
// show the desired form
document.body.querySelector(`div[data-form-id=&quot;${formName}&quot;]`).style.display = &#39;block&#39;;
}
});
})()

<!-- language: lang-css -->

/* Hide all forms at first until a button is clicked */
div[data-form-id]{
display: none;
}
/* just putting all the buttons in a row */
.new__btn {
display: inline-block;
}

<!-- language: lang-html -->

&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;catBtn&quot; data-target-form=&quot;categories&quot;&gt;New Category&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;blogBtn&quot; data-target-form=&quot;blogs&quot;&gt;New Blog&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;videoBtn&quot; data-target-form=&quot;videos&quot;&gt;New Video&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;new__btn&quot;&gt;
&lt;button id=&quot;prodBtn&quot; data-target-form=&quot;products&quot;&gt;New Product&lt;/button&gt;
&lt;/div&gt;
&lt;div id=&quot;catForm&quot; data-form-id=&quot;categories&quot;&gt;
Hide or show this div for the categories
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div data-form-id=&quot;blogs&quot;&gt;
Hide or show this div for the blogs
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div data-form-id=&quot;videos&quot;&gt;
Hide or show this div for the videos
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div data-form-id=&quot;products&quot;&gt;
Hide or show this div for the products
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

如果您将按钮和表单包装在包含元素中,并为按钮和表单添加数据属性,您可以使用事件委托。这允许您将一个监听器附加到按钮容器,该监听器将捕获其子元素触发的事件,因为它们在DOM中"冒泡"。

处理程序然后可以检查被点击的是否是按钮,提取其数据id(它将对应于表单上相同的数据id),并使用该id在选择器中定位该表单。

然后,只需使用CSS来切换"show"类。

正如您从示例中看到的,您可以使用相同的方法将活动类切换到点击的按钮上。

// 缓存表单和按钮容器
const forms = document.querySelector('.forms');
const buttons = document.querySelector('.buttons');

// 向按钮容器添加一个监听器
buttons.addEventListener('click', handleButton);

// 从一组元素中移除类的实用程序助手
function removeClasses(context, selector, classname) {
  context.querySelectorAll(selector).forEach(el => {
    el.classList.remove(classname);
  });
}

function handleButton(e) {
  
  // 如果监听器捕获到的点击元素是按钮
  if (e.target.matches('button')) {
    
    // 从其数据集中解构其id
    const { id } = e.target.dataset;

    // 使用模板字符串创建一个选择器,该选择器将查找具有匹配的data-id属性的表单
    const selector = `.form[data-id="${id}"]`;

    // 在表单容器中找到相应的表单
    const form = forms.querySelector(selector);

    // 如果按钮是活动的,请从按钮中删除活动类,并从表单中删除show类
    if (e.target.classList.contains('active')) {
      e.target.classList.remove('active');
      form.classList.remove('show');
    // 否则,请从按钮中删除所有活动类,从表单中删除所有show类,然后将活动类添加到按钮中,并将show类添加到所选的表单中。
    } else {
      removeClasses(buttons, 'button', 'active');
      removeClasses(forms, '.form', 'show');
      e.target.classList.add('active');
      form.classList.add('show');
    }
  }
}
.forms { margin-top: 1em; }
.form { display: none; border: 1px solid lightgray; padding: 0.5em; }
.form { margin-top: 0.5em; }
.show { display: block; }
button:hover { cursor: pointer; }
button:hover:not(.active) { background-color: #fffff0; }
.active { background-color: #ffff00; }
<div class="buttons">
  <button type="button" data-id="cat">New Category</button>
  <button type="button" data-id="blog">New Blog</button>
  <button type="button" data-id="video">New Video</button>
  <button type="button" data-id="prod">New Product</button>
</div>

<section class="forms">
  <div class="form" data-id="cat">
    隐藏或显示此类别的div
    <div class="panel">
      <div class="panel__head">Panel Head</div>
      <div class="panel__body">Panel Body</div>
      <div class="panel__footer">Panel Footer</div>
    </div>
  </div>
  <div class="form" data-id="blog">
    隐藏或显示博客的div
    <div class="panel">
      <div class="panel__head">Panel Head</div>
      <div class="panel__body">Panel Body</div>
      <div class="panel__footer">Panel Footer</div>
    </div>
  </div>
  <div>
    <div class="form" data-id="video">
      隐藏或显示视频的div
      <div class="panel">
        <div class="panel__head">Panel Head</div>
        <div class="panel__body">Panel Body</div>
        <div class="panel__footer">Panel Footer</div>
      </div>
    </div>
  </div>
  <div class="form" data-id="prod">
    隐藏或显示产品的div
    <div class="panel">
      <div class="panel__head">Panel Head</div>
      <div class="panel__body">Panel Body</div>
      <div class="panel__footer">Panel Footer</div>
    </div>
  </div>
</section>

附加文档

英文:

If you wrap your buttons and forms in containing elements, and add data attributes to both the buttons and forms you can use event delegation. This allows you to attach one listener to the buttons container which will catch events from its child elements as they "bubble up" the DOM.

The handler can then check that it was a button that was clicked, extract its data id (which will correspond to an identical data id on a form), and use that id in a selector to target that form.

Then it's just a matter of using CSS to toggle the "show" class.

As you can see from the example you can use the same method to toggle an active class to the clicked button too.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Cache the forms and buttons containers
const forms = document.querySelector(&#39;.forms&#39;);
const buttons = document.querySelector(&#39;.buttons&#39;);
// Add one listener to the buttons container
buttons.addEventListener(&#39;click&#39;, handleButton);
// Utility helper to remove classes from a set of elements
function removeClasses(context, selector, classname) {
context.querySelectorAll(selector).forEach(el =&gt; {
el.classList.remove(classname);
});
}
function handleButton(e) {
// If the clicked element that the listener catches
// is a button
if (e.target.matches(&#39;button&#39;)) {
// Destructure its id from its dataset
const { id } = e.target.dataset;
// Create a selector using a template string which
// will find a form with a matching data-id attribute
const selector = `.form[data-id=&quot;${id}&quot;]`;
// Find the corresponding form in the forms container
const form = forms.querySelector(selector);
// If the button is active, remove the active
// class, and remove the show class from the form
if (e.target.classList.contains(&#39;active&#39;)) {
e.target.classList.remove(&#39;active&#39;);
form.classList.remove(&#39;show&#39;);
// Otherwise remove all the active classes from the buttons,
// and all the show classes from the forms, and then add the
// active class to the button, and the show class to the
// selected form.
} else {
removeClasses(buttons, &#39;button&#39;, &#39;active&#39;);
removeClasses(forms, &#39;.form&#39;, &#39;show&#39;);
e.target.classList.add(&#39;active&#39;);
form.classList.add(&#39;show&#39;);
}
}
}

<!-- language: lang-css -->

.forms { margin-top: 1em; }
.form { display: none; border: 1px solid lightgray; padding: 0.5em; }
.form { margin-top: 0.5em; }
.show { display: block; }
button:hover { cursor: pointer; }
button:hover:not(.active) { background-color: #fffff0; }
.active { background-color: #ffff00; }

<!-- language: lang-html -->

&lt;div class=&quot;buttons&quot;&gt;
&lt;button type=&quot;button&quot; data-id=&quot;cat&quot;&gt;New Category&lt;/button&gt;
&lt;button type=&quot;button&quot; data-id=&quot;blog&quot;&gt;New Blog&lt;/button&gt;
&lt;button type=&quot;button&quot; data-id=&quot;video&quot;&gt;New Video&lt;/button&gt;
&lt;button type=&quot;button&quot; data-id=&quot;prod&quot;&gt;New Product&lt;/button&gt;
&lt;/div&gt;
&lt;section class=&quot;forms&quot;&gt;
&lt;div class=&quot;form&quot; data-id=&quot;cat&quot;&gt;
Hide or show this div for the categories
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form&quot; data-id=&quot;blog&quot;&gt;
Hide or show this div for the blogs
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;form&quot; data-id=&quot;video&quot;&gt;
Hide or show this div for the videos
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form&quot; data-id=&quot;prod&quot;&gt;
Hide or show this div for the products
&lt;div class=&quot;panel&quot;&gt;
&lt;div class=&quot;panel__head&quot;&gt;Panel Head&lt;/div&gt;
&lt;div class=&quot;panel__body&quot;&gt;Panel Body&lt;/div&gt;
&lt;div class=&quot;panel__footer&quot;&gt;Panel Footer&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

Additional documenation

答案3

得分: 0

首先,你正在比较节点和字符串(e.target.id返回一个字符串),这将始终返回false。

其次,你正在将事件监听器应用于文档,这意味着你正在向整个文档添加事件监听器,无论你在文档的哪个位置点击,它都会触发。

你需要为每个按钮添加事件监听器。你可以像这样做:

button.addEventListener('click', (event) => {
  show(getForm(button.id))
})

你还可以使用 document.querySelectAll('button') 来选择所有的按钮元素,然后循环遍历这些元素,应用上述的事件监听器。

希望这有所帮助!

英文:

First of all you are comparing Nodes, with strings (e.target.id returns a string) which will always return false.

Second, you are applying an event listener to document, meaning you are adding an eventListener to the entire document, wherever you click it will trigger.

You need to add an eventListener to every button. You can do something like this:

<!-- begin snippet: js hide: false console: true babel: false -->


button.addEventListener(&#39;click&#39;, (event) =&gt; {
show(getForm(button.id))
})

You can also do a document.querySelectAll(&#39;button&#39;) and loop over the element, apply the event listener above.

Hope this helps!

答案4

得分: 0

前面的回答提到了解决这个问题的一种方法,但也可以按照你之前的方法,通过将事件侦听器附加到文档来检测点击事件。你只需将e.target.id替换为e.target,因为代码应该比较一个元素与另一个元素,而不是将 id 与元素进行比较。

document.addEventListener('click', function(e){
    if(e.target === catBtn) {
        // catForm.classList.toggle("showHide");
        console.log("you clicked the cat btn");
    } else if(e.target === blogBtn) {
        // blogForm.classList.toggle("showHide");
        console.log("you clicked the blog btn");
    } else if(e.target === videoBtn) {
        // videoForm.classList.toggle("showHide");
        console.log("you clicked the video btn");
    } else if(e.target === prodForm) {
        // prodForm.classList.toggle("showHide");
        console.log("you clicked the product btn");
    }
})
英文:

The previous answer covered one option to solve this problem, but the clicks could also be detected in the way you were using earlier, by attaching and event listener to the document. All you need to do is replace e.target.id with just e.target because the code should be comparing an element to an element instead of an id to an element.

document.addEventListener(&#39;click&#39;, function(e){
if(e.target === catBtn) {
// catForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the cat btn&quot;);
} else if(e.target === blogBtn) {
// blogForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the blog btn&quot;);
} else if(e.target === videoBtn) {
// videoForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the video btn&quot;);
} else if(e.target === prodForm) {
// prodForm.classList.toggle(&quot;showHide&quot;);
console.log(&quot;you clicked the product btn&quot;);
}
})

答案5

得分: 0

"ok i solved it, with a little research and fine tuning i was able to use this to make everything work as i needed it. So in case anyone else faces this issue here is the code i used

function toggle_form(id) {
var e = document.getElementById(id);    
var allForms = document.querySelectorAll('div[id^=new]');
[].forEach.call(allForms, function(div) {  
if (div != e) {
div.style.display = 'none';
}
else {
e.style.display = e.style.display == 'none' ? 'block' : 'none';
}
});
}"
英文:

ok i solved it, with a little research and fine tuning i was able to use this to make everything work as i needed it. So in case anyone else faces this issue here is the code i used

function toggle_form(id) {
var e = document.getElementById(id);    
var allForms = document.querySelectorAll(&#39;div[id^=new]&#39;);
[].forEach.call(allForms, function(div) {  
if (div != e) {
div.style.display = &#39;none&#39;;
}
else {
e.style.display = e.style.display == &#39;none&#39; ? &#39;block&#39; : &#39;none&#39;;
}
});
}

huangapple
  • 本文由 发表于 2023年3月4日 06:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定