根据脚本状态更改文本(展开或折叠)

时间:2012-08-15 17:35:25

标签: jquery css

我正在尝试将静态“显示/隐藏公告”标题更改为动态“显示公告”或“隐藏公告”,具体取决于其是折叠还是展开。目前它只显示“显示/隐藏公告”,无论内容是折叠还是展开,我想将其更改为动态。

我的代码:

    <div class="collapsibleContainer" title="Show/Hide Announcement">
        <img src="images/announce.jpg" />
    </div>

我的剧本:

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $(".collapsibleContainer").collapsiblePanel();
    });
</script>
<script>
(function($) {
    $.fn.extend({
        collapsiblePanel: function() {
        // Call the ConfigureCollapsiblePanel function for the selected element
        return $(this).each(ConfigureCollapsiblePanel);
    }
  });
})(jQuery);


function ConfigureCollapsiblePanel() {
    $(this).addClass("ui-widget");

    // Check if there are any child elements, if not then wrap the inner text within a new div.
    if ($(this).children().length == 0) {
    $(this).wrapInner("<div></div>");
    }    

    // Wrap the contents of the container within a new div.
    $(this).children().wrapAll("<div class='collapsibleContainerContent ui-widget-content'></div>");

    // Create a new div as the first item within the container.  Put the title of the panel in here.
    $("<div class='collapsibleContainerTitle ui-widget-header'><div>" + $(this).attr("title") + "</div></div>").prependTo($(this));



    // Assign a call to CollapsibleContainerTitleOnClick for the click event of the new title div.
    $(".collapsibleContainerTitle", this).click(CollapsibleContainerTitleOnClick);
}


function CollapsibleContainerTitleOnClick() {
    // The item clicked is the title div... get this parent (the overall container) and toggle the content within it.
    $(".collapsibleContainerContent", $(this).parent()).slideToggle();
}

</script>

我的CSS:

.collapsibleContainer
{
    margin: 0 auto;
    width: 550px;
    padding-bottom: 10px;

}

.collapsibleContainerTitle
{
    cursor:pointer;
}

.collapsibleContainerTitle div
{
    padding-top:5px;
    padding-left:10px;
    color:#FFFFFF;
    font-family: 'Capriola';
    font-weight: 400;
    background-images: url(images/bg.png);
}

我更新的CSS显示不透明度,但它会更改文本。我只希望背景和边框淡出。悬停在1.0:

.collapsibleContainerTitle div
{
    padding-top:5px;
    padding-left:10px;
    color:#FFFFFF;
    font-family: 'Capriola';
    font-weight: 400;
    background-images: url(images/bg.png);
opacity:0.4;
filter:alpha(opacity=40); /* For IE8 and earlier */
}

.collapsibleContainerTitle div:hover
{
    padding-top:5px;
    padding-left:10px;
    color:#FFFFFF;
    font-family: 'Capriola';
    font-weight: 400;
    background-images: url(images/bg.png);
opacity:1.0;
filter:alpha(opacity=100); /* For IE8 and earlier */
}

2 个答案:

答案 0 :(得分:1)

您必须移动此脚本块

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $(".collapsibleContainer").collapsiblePanel();
    });
</script>

低于第二块剧本。

这是小提琴:

http://jsfiddle.net/EWJby/

答案 1 :(得分:1)

这样的事情:

function CollapsibleContainerTitleOnClick() {
    $(".collapsibleContainerContent", $(this).parent()).slideToggle(function() {
        $(".collapsibleContainerTitle", $(this).parent()).children(':first')
           .text($(this).is(':visible')?'Hide Announcement':'Show Announcement');
    });
}

FIDDLE

相关问题