0

你好,编码不是很熟练。当点击 .background-button 时,我的 #content 淡出和淡入。如何在按钮上获取文本以显示隐藏内容/显示内容?

我还需要在单击隐藏内容时淡出另外两个 div(.button-next、.button-prev)?

有谁能帮忙吗?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>


<script type="text/javascript">

var openingdelay = 200; // Delay before fade in initiated (default is 200)
var closingdelay = 100; // Delay before fade out initiated (default is 100)
var openingspeed = 25; // Speed of fade in (default is 25)
var closingspeed = 15; // Speed of fade out (default is 15)

function Show(elementid) {
    ele = document.getElementById(elementid);
        if(ele.style.display == 'none') {
            ele.style.opacity = 0;
            ele.style.filter = 'alpha(opacity=0)';
            ele.style.display = '';
            valueop = 1;
            setTimeout("fadeIn()", openingdelay);
        } 
        else {
            valueop = 9;
            setTimeout("fadeOut()", closingdelay);
        }
}
        function fadeOut() {
        if(valueop < 1) {
        ele.style.display = 'none';
        return false;
        }
            ele.style.opacity = valueop/10;
            ele.style.filter = 'alpha(opacity='+(valueop*10)+')';
            valueop = valueop - 1;
            setTimeout("fadeOut()", closingspeed);
        }
        function fadeIn() {
        if(valueop > 10) {
        return false;
        }
            ele.style.opacity = valueop/10;
            ele.style.filter = 'alpha(opacity='+(valueop*10)+')';
            valueop = valueop + 1;
            setTimeout("fadeIn()", openingspeed);
        } 


</script>
<style type="text/css">
.layer1 {
margin: 0;
padding: 0;
width: 500px;
}

.background-button {
margin: 1px;
color: #fff;
padding: 3px 10px;
cursor: pointer;
position: relative;
background-color:#c30;
float:right;
}
#content {
padding: 5px 10px;
background-color:#000;
float:left; width:400px; height:600px; color:#FFF
}

</style>
</head>

<body>

<div id="content" >Hello! This will remain hidden until the link is clicked.</div>

<div class="background-button"><a href="#" onClick="Show('content'); return false;">Click Me To Reveal More</a></div>

<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:100px; height:50px; width:100px;" id="button-next">next</div>
<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:200px; height:50px; width:100px;" id="button-prev">prev</div>


</body>
</html>
4

2 回答 2

0

我将使用注册而不是内联事件处理程序添加事件侦听器:

var button = document.querySelector('.background-button');
button.addEventListener("click", function(){
    Show('content', this.children[0]);
    return false;
});

该函数Show相应地必须修改为:

function Show(elementid, link) {
    ele = document.getElementById(elementid);
    if (ele.style.display == 'none') {
        ele.style.opacity = 0;
        ele.style.filter = 'alpha(opacity=0)';
        ele.style.display = '';
        valueop = 1;
        setTimeout("fadeIn()", openingdelay);
    }
    else {
        valueop = 9;
        setTimeout("fadeOut()", closingdelay);
    }
    if(link.getAttribute('data-hidden') == "hidden"){
        link.innerHTML = "Click Me To Hide Stuff";
        link.removeAttribute("data-hidden");
    }else{
        link.innerHTML = "Click Me To Reveal More";
        link.setAttribute("data-hidden", "hidden");
    }
}

结果如下:http: //jsfiddle.net/p3Qzk/

于 2012-12-11T11:57:26.267 回答
0

我修改了您的 html 以将和 ID 放入链接:#show_more

HTML:

<div id="content" >Hello! This will remain hidden until the link is clicked.</div>

<div class="background-button"><a id="show_more" href="#">Click Me To Reveal More</a></div>

<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:100px; height:50px; width:100px;" id="button-next">next</div>
<div style="background-color:#000; color:#FFF; position:absolute; right:0px; top:200px; height:50px; width:100px;" id="button-prev">prev</div>​

jQuery:

$('#content, #button-next, #button-prev').hide();
$('#show_more').click(function(e) {
    e.preventDefault(); // disable default <a> behavior

    if ($('#content').is(':visible')) {
        $('#show_more').text('Show Content');
    } else {
        $('#show_more').text('Hide Content');
    }

    $('#content, #button-next, #button-prev').fadeToggle();
});

第一行只是隐藏了内容和按钮。您应该通过 CSS 或内联样式隐藏它们并删除这行代码。

编辑:jsFiddle

于 2012-12-11T11:59:05.553 回答