简体   繁体   English

Onclick查找href并在div中生成新内容

[英]Onclick find href and generate new content in div

Hey so I have a navigation menu and what I want to do is show/hide the content inside the div based on which link in the navigation you click. 嘿,我有一个导航菜单,我想要做的是根据您单击导航中的哪个链接来显示/隐藏div中的内容。 I want the jQuery/javascript to find the href=#panel21 and use the #panel21 to show that div of content. 我希望jQuery / Javascript找到href =#panel21并使用#panel21来显示该div内容。

I am doing this right now by adding a class to each a tag and calling each one in a separate jQuery function. 我现在正在通过向每个标签添加一个类并在单独的jQuery函数中调用每个标签来进行此操作。 But as I add more links to the nav this is going to get out of hand, so I need to simplify. 但是随着我向导航栏添加更多链接,这将变得一发不可收拾,因此我需要简化。 My current project can be found on Codepen 我当前的项目可以在Codepen找到

And here is the snippet 这是片段

 // open mobile menu $('.js-toggle-menu').click(function(e){ $('.mobile-header-nav').slideToggle(); $(this).toggleClass('open'); }); $('.sub-toggle').click(function(e){ $(this).next('.subnav').slideToggle(); $(this).toggleClass('open'); }); $('.panel1').click(function(){ $('.newContent').html($('#panel11').html());}); $('.panel2').click(function(){ $('.newContent').html($('#panel21').html());}); $('.panel3').click(function(){ $('.newContent').html($('#panel31').html());}); $('.panel4').click(function(){ $('.newContent').html($('#panel41').html());}); $('.panel5').click(function(){ $('.newContent').html($('#panel51').html());}); 
 * { box-sizing: border-box; } @media (min-width: 768px) { .mobile-nav-wrap { /* display: none; */ } } .mobile-header-nav { background-color: #222222; display: none; list-style: none; margin: 0; padding: 0; width: 100%; } .mobile-header-nav li { border-bottom: 1px solid rgba(255, 255, 255, 0.1); } .mobile-header-nav li a { color: white; display: block; padding: 15px 15px; text-align: left; text-decoration: none; -webkit-transition: all 0.3s ease-in-out; transition: all 0.3s ease-in-out; } .mobile-header-nav li a:hover { background-color: #2f2f2f; } a.mobile-menu-toggle { padding-left: 50px; color: #52575f; text-decoration: none; background: #eeeff0; font-size: 3em; } .subnav { display: none; } #panel11, #panel21, #panel31, #panel41, #panel51 { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <header> <nav class="mobile-nav-wrap" role="navigation"> <ul class="mobile-header-nav"> <li> <a href="#" class="sub-toggle">Overview</a> <ul class="subnav"> <li><a class="panel1" href="#panel11">Nav Item 1</a></li> <li><a class="panel2" href="#panel21">Nav Item 2</a></li> <li><a class="panel3" href="#panel31">Nav Item 3</a></li> </ul> </li> <li><a class="sub-toggle" href="#">Resources</a> <ul class="subnav"> <li><a class="panel4" href="#panel41">Nav Item 1</a></li> <li><a class="panel5" href="#panel51">Nav Item 2</a></li> <li><a href="#">Nav Item 3</a></li> </ul> </li> <li><a class="sub-toggle" href="#">Service</a> <ul class="subnav"> <li><a href="#">Nav Item 1</a></li> <li><a href="#">Nav Item 2</a></li> <li><a href="#">Nav Item 3</a></li> </ul> </li> </ul> </nav> <a class="mobile-menu-toggle js-toggle-menu" href="#"> Get Started </a> </header> <div class="mainContent"> <div id="panel11" class="content"> <h2>Panel 1 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel21" class="content"> <h2>Panel 2 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel31" class="content"> <h2>Panel 3 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel41" class="content"> <h2>Panel 4 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel51" class="content"> <h2>Panel 5 Content</h2> <p>Lorem ipsum stuff here</p> </div> </div> <div class="newContent" id="linkContent"> <p>The new content will show up here</p> </div> 

I removed all of the click handlers and added in the end: 我删除了所有点击处理程序,最后添加了:

function handleMenuClick(e){
  e.stopPropagation();
  $('.newContent').html($($(this).attr("href")).html());
}
$("ul.mobile-header-nav").on("click", ".subnav li a",handleMenuClick);

this way you have event delegation even for dynamically created content 这样,您甚至可以针对动态创建的内容进行事件委托

http://codepen.io/Saar/pen/Vvradp http://codepen.io/Saar/pen/Vvradp

You don't need a different class for each. 您不需要为每个类分配不同的类。 Try this: 尝试这个:

$('.panel').click(function(){
$('.newContent').html($($(this).attr('id')+'1').html());});

and just use class panel for all of them and use what you now have as class as id. 并使用所有类的panel ,并使用您现在作为id的类。

event better to use the id instead of the class for the target: 事件最好使用id代替目标类:

$('.panel').click(function(){
$('#linkContent').html($($(this).attr('id')+'1').html());});

other way is : https://jsfiddle.net/mig1098/1yr4zthe/ 其他方式是: https : //jsfiddle.net/mig1098/1yr4zthe/

$('.mobile-header-nav li a[class^="panel"]').click(function(){
    $('.newContent').html($($(this).attr('href')).html());
});

Use single function in click as: 单击中使用单个功能为:

$('.panel1, .panel2, .panel3, .panel4, .panel5').click(function(){
    var id = $(this).attr("class") + "1";
    $('.newContent').html($(id).html());
});

 // open mobile menu $('.js-toggle-menu').click(function(e){ $('.mobile-header-nav').slideToggle(); $(this).toggleClass('open'); }); $('.sub-toggle').click(function(e){ $(this).next('.subnav').slideToggle(); $(this).toggleClass('open'); }); $('.panel1, .panel2, .panel3, .panel4, .panel5').click(function(){ var id = $(this).attr("class") + "1"; $('.newContent').html($("#" + id).html()); }); 
 * { box-sizing: border-box; } @media (min-width: 768px) { .mobile-nav-wrap { /* display: none; */ } } .mobile-header-nav { background-color: #222222; display: none; list-style: none; margin: 0; padding: 0; width: 100%; } .mobile-header-nav li { border-bottom: 1px solid rgba(255, 255, 255, 0.1); } .mobile-header-nav li a { color: white; display: block; padding: 15px 15px; text-align: left; text-decoration: none; -webkit-transition: all 0.3s ease-in-out; transition: all 0.3s ease-in-out; } .mobile-header-nav li a:hover { background-color: #2f2f2f; } a.mobile-menu-toggle { padding-left: 50px; color: #52575f; text-decoration: none; background: #eeeff0; font-size: 3em; } .subnav { display: none; } #panel11, #panel21, #panel31, #panel41, #panel51 { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <header> <nav class="mobile-nav-wrap" role="navigation"> <ul class="mobile-header-nav"> <li> <a href="#" class="sub-toggle">Overview</a> <ul class="subnav"> <li><a class="panel1" href="#panel11">Nav Item 1</a></li> <li><a class="panel2" href="#panel21">Nav Item 2</a></li> <li><a class="panel3" href="#panel31">Nav Item 3</a></li> </ul> </li> <li><a class="sub-toggle" href="#">Resources</a> <ul class="subnav"> <li><a class="panel4" href="#panel41">Nav Item 1</a></li> <li><a class="panel5" href="#panel51">Nav Item 2</a></li> <li><a href="#">Nav Item 3</a></li> </ul> </li> <li><a class="sub-toggle" href="#">Service</a> <ul class="subnav"> <li><a href="#">Nav Item 1</a></li> <li><a href="#">Nav Item 2</a></li> <li><a href="#">Nav Item 3</a></li> </ul> </li> </ul> </nav> <a class="mobile-menu-toggle js-toggle-menu" href="#"> Get Started </a> </header> <div class="mainContent"> <div id="panel11" class="content"> <h2>Panel 1 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel21" class="content"> <h2>Panel 2 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel31" class="content"> <h2>Panel 3 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel41" class="content"> <h2>Panel 4 Content</h2> <p>Lorem ipsum stuff here</p> </div> <div id="panel51" class="content"> <h2>Panel 5 Content</h2> <p>Lorem ipsum stuff here</p> </div> </div> <div class="newContent" id="linkContent"> <p>The new content will show up here</p> </div> 

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

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