简体   繁体   English

单击javascript / jquery中的文本后如何启用选项卡及其内容?

[英]How to enable tab and its content on click of a text in javascript/jquery?

I am working on a website on which I want enable button on click of a text in Javascript/Jquery. 我在一个网站上工作,我想在其中单击Javascript / Jquery中的文本时启用按钮。

The JQuery code which I have used in order to scroll down towards the bottom on clicking view options text are: 我用来在单击view options文本时向下滚动到底部的JQuery代码是:

$(".view_profile_options a").click(function(event) {
    event.preventDefault();
    var sectionTop = $('.tab-contents').offset().top;
    $('html,body').animate({
        scrollTop: sectionTop },
        'slow');
 });


Problem Statement: 问题陈述:

I am wondering what changes I should make in the JQuery code above so that on clicking view options text in the fiddle , the hover for tokyo and its content gets enabled automatically. 我想知道我应该在上面的JQuery代码中进行哪些更改,以便在小提琴中单击view options文本时, tokyo的悬停及其content会自动启用。

This works like a charm: 这就像一个魅力:

 $(".view_profile_options a").click(function(event) { event.preventDefault(); var sectionTop = $('.tab-contents').offset().top; var event = $.Event('click'); event.currentTarget = $('.tab .tablinks')[2]; $('.tab .tablinks:nth-child(3)').trigger(event); $('html,body').animate({ scrollTop: sectionTop }, 'slow'); }); 

You need to simulate clicking on the button. 您需要模拟单击按钮。 In your case as you have no button ID, I use selector by containing the desired text. 对于您的情况,因为您没有按钮ID,所以我通过包含所需的文本来使用选择器。

I have also removed your complex JavaScript Codes and wrote a simple Jquery instead to hide and show tabs and buttons: 我还删除了复杂的JavaScript代码,而是编写了一个简单的Jquery来隐藏和显示选项卡和按钮:

 $(document).ready(function(){ $(".view_profile_options a").click(function(event) { event.preventDefault(); var sectionTop = $('.tab-contents').offset().top; $('html,body').animate({ scrollTop: sectionTop }, 'slow'); $('button:contains("Tokyo")').trigger("click"); }); $("button").click(function(){ //Display tab var city=$(this).text(); $(".tabcontent").hide(); $("#"+city).show(); // Change class of button $("button").removeClass("active"); $(this).addClass("active"); }) }) 
 .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ .tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ .tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } /* Style the tab content */ .tab-contents>.active { display:block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="ml-2 mb-2 d-flex view_profile_options"><a href=""> view options</a> <i class="ml-2 fas fa-2x fa-angle-right"></i></p> <p>Click on the buttons inside the tabbed menu:</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <div class="tab"> <button class="tablinks active">London</button> <button class="tablinks">Paris</button> <button class="tablinks"">Tokyo</button> </div> <div class="tab-contents"> <div id="London" class="tabcontent active"> <h3>London</h3> <p>London is the capital city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Paris</h3> <p>Paris is the capital of France.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>Tokyo</h3> <p>Tokyo is the capital of Japan.</p> </div> </div> <script> </script> 

You could do it like that, using: 您可以这样使用:

  • A new attribute to target the button element, 定位按钮元素的新属性,
  • Then, use .prop("disabled", false) on that button. 然后,在该按钮上使用.prop("disabled", false)

Working snippet: 工作片段:

 $(".view_profile_options a").click(function(event) { event.preventDefault(); var id = "#" + $(this).attr("target"); $(id).prop("disabled", false); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="view_profile_options" > <a target="button1">Click me!</a> </div> <button id="button1" disabled>My button</button> 

Hope it helps. 希望能帮助到你。

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

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