javascript 如何在点击按钮时更改页面内容或分区内容[关闭]

nvbavucw  于 2023-09-29  发布在  Java
关注(0)|答案(2)|浏览(85)

已关闭,此问题需要更focused。它目前不接受回答。
**想改善这个问题吗?**更新问题,使其只关注editing this post的一个问题。

关闭7天前。
Improve this question
我非常初学者的UI,我试图改变页面内容动态按钮点击如下:
Image Reference
有人能建议我如何才能实现这种类型的功能

h9vpoimq

h9vpoimq1#

下面是一个动态改变内容的简单例子,其中的逻辑是使用forEach()方法用JavaScript编写的。forEach()方法需要从数组中计算当前元素。
如果你有任何问题,就问我。

let button = document.querySelectorAll('.menu button');
let content_inside = document.querySelectorAll('.content_inside');

Array.from(button).forEach(function(buttonArray, i) {
buttonArray.addEventListener('click', function() {

    Array.from(button).forEach(buttonAll => buttonAll.classList.remove('button_active'));
    
    Array.from(content_inside).forEach(content_insideAll => content_insideAll.classList.remove('content_inside_active'));
    
    button[i].classList.add('button_active'); 
    
    content_inside[i].classList.add('content_inside_active');  
  });
});
.menu {
  display: flex;
}

.menu button {
  margin: 10px;
  cursor: pointer;
}

.button_active {
  color: teal;
}

.content_inside {
  display: none;
  border: 1px solid #000000;
  border-radius: 10px;
  padding: 10px;
}

.content_inside_active {
  display: block;
}
<div class="container">
  <div class="menu">
    <button class="button_active">Content 1</button>
    <button>Content 2</button>
    <button>Content 3</button>
  </div>
  <div class="content">
    <div class="content_inside content_inside_active">
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    </div>
    <div class="content_inside">
    "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
    </div>
    <div class="content_inside">
    "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
    </div>
  </div>
</div>
thtygnil

thtygnil2#

使用Jquery来显示和隐藏。范例:

function english_btn(){
$("#english").css("display","block");
$("#french").css("display","none");
$("#laing").css("display","none");
}

相关问题