[英]html div element on click getting appened
I have a vertical menu where there are 5 items webdesign, mobile app etc., as shown below and when clicked on that it shows the respective href attribute. 我有一个垂直菜单,其中包含5个项目网页设计,移动应用程序等,如下所示,单击该菜单时会显示相应的href属性。
<div class="col-md-3">
<ul class="nav nav-tabs nav-stacked">
<li class="active">
<a href="#web" onclick="webdesDiv()">Web Design and Development</a> </li>
<li><a href="#mob" onclick="mobileDiv()">Mobile Applicaiton Development</a></li>
<li><a href="#ecom" onclick="ecommDiv()">eCommerce Solutions </a></li>
<li><a href="#soft" onclick="softDiv()">Software development</a></li>
<li><a href="#supp" onclick="supportDiv()">Support & Maintenance</a></li>
</ul>
</div>
<div class="col-md-9" style="display:none;" id="webdesc">
<h4>This is how the webdesign page looks like</h4>
<p>Secure and highly interactive eCommerce storefront that is designed to leverage your product sales is developed using the latest coding techniques in the industry.</p>
</div>
<div class="col-md-9" style="display:none;" id="mobdesc">
<h4>This is how the mobile app section page looks like</h4>
<p>Secure and highly interactive eCommerce storefront that is designed to leverage your product sales is developed using the latest coding techniques in the industry.</p>
</div>
The problem is, when i click on Mobile application development, respective description is coming correctly, where as when i click on other option ex: ecommerce solutions, result is getting appended in same page. 问题是,当我单击“移动应用程序开发”时,相应的描述正确显示,而当我单击其他选项时,例如:电子商务解决方案,结果被附加在同一页面中。 I need only that particular description rather than getting result appended.
我只需要该特定描述,而不需要附加结果。 Kindly suggest fixes in my code .
请在我的代码中提出修复建议。
My javascript code : 我的JavaScript代码:
<script>
function mobileDiv()
{
document.getElementById('mobdesc').style.display = "block";
}
function webdesDiv()
{
document.getElementById('webdesc').style.display = "block";
}
</script>
Try this Way : 尝试这种方式:
<script>
function mobileDiv()
{
document.getElementById('mobdesc').style.display = "block";
document.getElementById('webdesc').style.display = "none";
}
function webdesDiv()
{
document.getElementById('webdesc').style.display = "block";
document.getElementById('mobdesc').style.display = "none";
}
</script>
Make only one div to display:block
and rest of other display:none
仅使一个div
display:block
,其余display:none
其他display:none
You need to add display:none for the blocks other than the one you clicked. 您需要为所有块添加display:none,而不是单击的块。
Your script should look something like this: 您的脚本应如下所示:
function mobileDiv() { document.getElementById('mobdesc').style.display = "block"; document.getElementById('webdesc').style.display = "none"; } function webdesDiv() { document.getElementById('webdesc').style.display = "block"; document.getElementById('mobdesc').style.display = "none" }
<div class="col-md-3"> <ul class="nav nav-tabs nav-stacked"> <li class="active"> <a href="#web" onclick="webdesDiv()">Web Design and Development</a> </li> <li><a href="#mob" onclick="mobileDiv()">Mobile Applicaiton Development</a></li> <li><a href="#ecom" onclick="ecommDiv()">eCommerce Solutions </a></li> <li><a href="#soft" onclick="softDiv()">Software development</a></li> <li><a href="#supp" onclick="supportDiv()">Support & Maintenance</a></li> </ul> </div> <div class="col-md-9" style="display:none;" id="webdesc"> <h4>This is how the webdesign page looks like</h4> <p>Secure and highly interactive eCommerce storefront that is designed to leverage your product sales is developed using the latest coding techniques in the industry.</p> </div> <div class="col-md-9" style="display:none;" id="mobdesc"> <h4>This is how the mobile app section page looks like</h4> <p>Secure and highly interactive eCommerce storefront that is designed to leverage your product sales is developed using the latest coding techniques in the industry.</p> </div>
Apply display:none to the blocks other than the one which is clicked. 将display :: none应用于单击的块以外的块。
A small suggestion: Since it looks like you're using bootstrap, use bootstrap navigation component - http://getbootstrap.com/components/#nav . 一个小建议:由于看起来您正在使用引导程序,因此请使用引导程序导航组件-http://getbootstrap.com/components/#nav 。
You need to hide the non-active div 您需要隐藏无效的div
If I were to code this in plain JS, I would not use hashes that go nowhere but make them useful. 如果要用普通的JS编写此代码,则不会使用散乱无章的哈希,而只会使它们有用。 The following unobtrusively adds the event handlers to each link and use their hash to show/hide the repective div and to activate/deactivate the parent LI
以下内容毫不干扰地将事件处理程序添加到每个链接,并使用其哈希值显示/隐藏相应的div并激活/停用父LI
window.onload = function() { var navLinks = document.querySelectorAll(".nav li a"); // get all nav links for (var i = 0; i < navLinks.length; i++) { // loop over them navLinks[i].onclick = function() { // assign click handlers var activeLink = document.querySelector(".nav > li.active > a"), activeDivId = activeLink.hash.substring(1), activeLI = activeLink.parentNode; document.getElementById(activeDivId).style.display = "none"; // hide active div activeLI.className = ""; // make LI non-active activeDivId = this.hash.substring(1); // find new div ID document.getElementById(activeDivId).style.display = "block"; // show it this.parentNode.className = "active"; // activate the LI return false; // cancel the click } } document.querySelector(".nav > li.active > a").click(); // show active link }
.active { background-color: yellow }
<div class="col-md-3"> <ul class="nav nav-tabs nav-stacked"> <li class="active"><a href="#webdesc">Web Design and Development</a> <div class="col-md-9" style="display:none;" id="webdesc"> <h4>This is how the webdesign page looks like</h4> <p>Secure and highly interactive eCommerce storefront that is designed to leverage your product sales is developed using the latest coding techniques in the industry.</p> </div> </li> <li><a href="#mobdesc">Mobile Application Development</a> <div class="col-md-9" style="display:none;" id="mobdesc"> <h4>This is how the mobile app section page looks like</h4> <p>Secure and highly interactive eCommerce storefront that is designed to leverage your product sales is developed using the latest coding techniques in the industry.</p> </div> </li> </ul> </div>
jQuery if needed jQuery(如果需要)
$(function() {
var $navLinks = $(".nav li a"); // get all nav links
$navLinks.each(function() { // loop over them
$(this).on("click", function(e) { // assign click handlers
e.preventDefault();
var $activeLink = $(".nav > li.active > a"),
$activeLI = $activeLink.parent();
$($activeLink.prop("hash")).hide(); // hide active div
$activeLI.removeClass("active"); // make LI non-active
$(this.hash).show(); // show it
$(this).parent().addClass("active"); // activate the LI
});
});
$(".nav > li.active > a").click(); // show active link
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.