简体   繁体   English

带有onclick的标签会激活其他CSS样式并显示其特定内容

[英]Tabs with onclick activates other css style AND shows it's specific content

I'm very new to all the Java/Jquery, but for my site I want to use it. 我对所有Java / Jquery都是新手,但是对于我的网站,我想使用它。

There's this problem; 有这个问题 I'm trying to make some fancy tab-style navigation. 我正在尝试制作一些精美的标签样式导航。 The point is that if I use the "onclick" in HTML5 it won't do more than one activity. 关键是,如果我在HTML5中使用“ onclick”,它只会执行一项活动。

What I have: 我有的:

HTML/JAVA/CSS: HTML / JAVA / CSS:

    <!-- this are the buttons to press for showing content. -->

        <div id="MENU1" class="active" onclick="showHide('1');"><center>1</center></div>
        <div id="MENU2" class="inactive" onclick="showHide('2');"><center>2</center></div>
        <div id="MENU3" class="inactive" onclick="showHide('3');"><center>3</center></div>
        <div id="MENU4" class="inactive" onclick="showHide('4');"><center>4</center></div>

        <div id="CONTAINER">
        <!-- here should appear text -->
        <div id="CONTENT1">here somehow wil appear content 1</div>
        <div id="CONTENT2">here somehow wil appear content 2</div>
        <div id="CONTENT3">here somehow wil appear content 3</div>
        <div id="CONTENT4">here somehow wil appear content 4</div>
        </div>

    <script type="text/javascript"> 
    function showHide(divId){
        var theDiv = document.getElementById(divId);
        if(theDiv.style.display=="none"){
            theDiv.style.display="block";
        }else{
            theDiv.style.display="none";
        }    
    }
    </script>


<!-- the style of the buttons have to change from .inactive to .active -->
    <style>
    .active{
        color:#FF6a00;
        background-image:url(images/blackbck.png);
        background-repeat:round;
        background-size:cover;
    }

    .inactive{
        color:black;
        background-image:url(images/orangebck.png);
        background-repeat:round;
        background-size:cover;
    }
    </style>

Could someone help me with this? 有人可以帮我吗?

Summary: 摘要:

  • The buttons should be changing styles. 按钮应更改样式。
  • Content should appear by clicking button. 内容应通过单击按钮出现。
  • By clicking some other button, the old content should be hidden. 通过单击其他按钮,旧内容应被隐藏。

Css for active 活跃的CSS

.active{ color:#FF6a00!important; background-image:url(images/blackbck.png)!important; background-repeat:round; background-size:cover; }

First add jQuery Library in head 首先在头添加jQuery库

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

Add class to all Contents like this: 将类添加到所有内容,如下所示:

<div id="CONTENT1" class="content">here somehow wil appear content 1</div>

Remove your onclick on all divs and add class button: 删除所有div上的onclick并添加类按钮:

<div id="MENU1" class="active inactive button"><center>1</center></div>

After try this: 尝试之后:

<script>
$(document).ready(function(){
    $('.content').css('display','none');
    $('#CONTENT1').css('display','');
    $('#MENU1').click(function(){
        $('.button').removeClass('active');            
        $(this).addClass('active');
        $('.content').fadeOut(300);
        $('#CONTENT1').fadeIn(300);
    });
    $('#MENU2').click(function(){
        $('.button').removeClass('active');            
        $(this).addClass('active');
        $('.content').fadeOut(300);
        $('#CONTENT2').fadeIn(300);
    });
    $('#MENU3').click(function(){
        $('.button').removeClass('active');            
        $(this).addClass('active');
        $('.content').fadeOut(300);
        $('#CONTENT3').fadeIn(300);
    });
    $('#MENU4').click(function(){
        $('.button').removeClass('active');            
        $(this).addClass('active');
        $('.content').fadeOut(300);
        $('#CONTENT4').fadeIn(300);
    });
});    

With fadeIn/Out effects. 具有淡入/淡出效果。 If you dont want change to .css('display','none') / .css('display','') 如果您不想更改为.css('display','none')/ .css('display','')

You have an error in your code, you are passing '1' to the function showHide() and then using document.getElementById('1'); 您的代码中有错误,正在将'1'传递给函数showHide(),然后使用document.getElementById('1'); there is no html element with that id, try to pass the content id of the div you want to show. 没有具有该ID的html元素,请尝试传递您要显示的div的内容ID。

<div id="MENU1" class="active" onclick="showHide('CONTENT1');"><center>1</center></div>

or do something like this. 或做这样的事情。

<div id="MENU1" class="active" onclick="showHide('1');"><center>1</center></div>

then in your javascript function do this. 然后在您的javascript函数中执行此操作。

var theDiv = document.getElementById('CONTENT'+divId);
if(theDiv.style.display=="none"){
    theDiv.style.display="block";
} else{
    theDiv.style.display="none";
}   

Just add this line after function showHide(divId) { : 只需在function showHide(divId) {之后添加此行:

var divId = 'CONTENT'+divId;

JSFIDDLE 的jsfiddle

you could do it also like this: 您也可以这样:

//HTML
<div data-tab="1" id="MENU1" class="tab-navi active" onclick="showHide('1');"><center>1</center></div>
<div data-tab="2" id="MENU2" class="tab-navi" onclick="showHide('2');"><center>2</center></div>

<div id="CONTAINER">
    <!-- here should appear text -->
    <div class="tab-content" id="CONTENT1">here somehow wil appear content 1</div>
    <div class="tab-content"  id="CONTENT2">here somehow wil appear content 2</div>
</div>
//JQUERY
$("div.tab-navi").on("click", function(){
    var $this = $(this);
    var id = $this.attr("data-tab");
    $("div.tab-content").hide(); //Hide all
    $(".tab-navi").removeClass("active");
    $("#CONTENT"+id).show();
    $this.addClass("active");
});
//CSS
.tab-navi.active{
    color:#FF6a00;
    background-image:url(images/blackbck.png);
    background-repeat:round;
    background-size:cover;
}

.tab-navi{
    color:black;
    background-image:url(images/orangebck.png);
    background-repeat:round;
    background-size:cover;
}

here the fiddle: http://jsfiddle.net/bs8gG/ 这里的小提琴: http : //jsfiddle.net/bs8gG/

As per what I have understood, 据我了解,

UPDATED JSFIDDLE DEMO 更新的JSFIDDLE演示

HTML HTML

<button id="MENU1" class="active" onclick="showHide('1');"><center>1</center></button>
        <button id="MENU2" class="inactive" onclick="showHide('2');"><center>2</center></button>
        <button id="MENU3" class="inactive" onclick="showHide('3');"><center>3</center></button>
        <button id="MENU4" class="inactive" onclick="showHide('4');"><center>4</center></button>

        <div id="CONTAINER"></div>

JS JS

function showHide(divId){
    $("button").removeClass("active");
    var id = "MENU" + divId;
    var theDiv = document.getElementById(id);
      $(theDiv).addClass("active"); 

    var data = "<div id='CONTENT'" + divId + ">here somehow wil appear content " + divId + "</div>";
    var print = document.getElementById("CONTAINER");
    print.innerHTML = data;   
}

Like the other guys pointed out - your first problem is here: 就像其他人指出的那样-您的第一个问题在这里:

var theDiv = document.getElementById(divId); var theDiv = document.getElementById(divId);

this should rather be: 这应该是:

var theDiv = document.getElementById("CONTENT" + divId); var theDiv = document.getElementById(“ CONTENT” + divId);

you pass just a "1" to the showHide function, and then you use document.getElementById to search for an element with an ID="1", instead of ID=”CONTENT1” 您只需将“ 1”传递给showHide函数,然后使用document.getElementById搜索ID =“ 1”而不是ID =“ CONTENT1”的元素

Secondly - and what the other guys didn't point out! 其次- 其他人没有指出什么! , the else{} block won't do what you expect it to do – it will just hide the div with the divId, instead of all other divs, Your function fixed: ,else {}块将无法执行您期望的操作–它只会使用divId隐藏div,而不是所有其他div,您的函数已修复:

function showHide(divId){

    //getting all content divs
    var divs = document.getElementById("CONTAINER").childNodes;
    //iterating each of the content divs
    for(var i = 0; i < divs.length; i++){
        //getting id of a current div
        var currentId = divs[i].getAttribute('id'); 
        //if the id fits we want to show the div
        if(currentId === "CONTENT" + divId ){
            divs[i].style.display="block";
        }else{ //otherwise hide it
            divs[i].style.display="none";
        }
    }
}

Mid you that I didn't deal with the style change here. 你们中间,我没有在这里处理样式更改。 Joao Paulo gave the right answer, but it involves jQuery. Joao Paulo给出了正确的答案,但这涉及到jQuery。

An offtopic: 题外话:

Instead of using the CENTER tag, always use styles! 不要使用CENTER标记,而要使用样式! In your case: 在您的情况下:

#MENU1, #MENU2, #MENU3, #MENU4{
text-align: center
}

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

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