javascript - 具有 HTML 文本更改的多个 SlideToggle Div

标签 javascript jquery html

基本上我有以下 JS,但想知道如何更有效地完成它,我似乎让事情变得过于复杂。我觉得我可以重用这些功能之一;

    $('.hide').hide(); //hides all the hide links on page load

 $('a.show').each(function() { //running iterator function
 var thisLink = $(this);
 $(this).click(function() { // Checks if any show link is clicked
 show(thisLink) // Calling the show function & passing the selector value
 });
 });

$('a.hide').each(function() { //running iterator function
 var thisLink = $(this);
 $(this).click(function() { // Checks if any hide link is clicked
 hide(thisLink) // Calling the hide function & passing the selector value
 });
 });

 });

function show(btn) {
btn.hide(); //hides the element where click was received
btn.next().show(); //displays the next element that in our case is hide link
btn.next().next().slideToggle('slow'); //toggles the next of next element that is details div
 }

function hide(btn) {
btn.hide(); //hides the element where click was received
btn.prev().show(); //displays the previous element that in our case is show link
btn.next().slideToggle('slow'); //again toggles the next element that is details div to hide it
 }

最佳答案

尝试,

 $('.hide').hide(); //hides all the hide links on page load

 $('a.show').click(show);

 $('a.hide').click(hide);

function show() {
  $(this).hide().next().show().next().slideToggle('slow'); 
}

function hide() {
  $(this).hide().prev().show();
  $(this).next().slideToggle('slow');
}

关于javascript - 具有 HTML 文本更改的多个 SlideToggle Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24202097/

相关文章:

javascript - 在 2 个标题 Logo 之间进行转换

javascript - 使用 document.forms ["MbrForm"].submit();不起作用

javascript - 如何使用javascript模拟css的悬停

html - 如何连续点击满足特定条件的链接?

javascript - 生成动态画廊jquery

javascript - $.extend() 不合并选项

javascript - 焦点在可编辑表格中不起作用

javascript - 带有多个slideToggle的jQuery

php - 将 PHP 生成的 JS 变量发送到 Google map 的 JS 函数

html - 页脚栏不起作用