jQueryUI Stackable(删除不同选项卡上的内容单击)

时间:2015-03-18 15:03:05

标签: javascript jquery jquery-ui tabs selectable

注意:我只有几天的jQuery经验,虽然我已经四处寻找尝试解决问题并进行测试但是它无法正常工作。

我想要实现的目标:

我在使用jQueryUI时遇到了一些问题。可选,我创建了一个带有四个不同标签的Selectable,每个标签显示添加到其中的不同内容(图像类,按钮的附加和段落文字的附加物)。

以下是我想要实现的目标: http://puu.sh/gELQi/211165f55a.png


我的问题

目前,当点击一个标签时,它会正确显示该标签的内容,但是只要您再次点击其他/相同标签,它就会:

  • 添加与上一个标签重叠的内容

    1. 每次点击相同或其他标签
    2. 时都会添加按钮
    3. 添加新图像类并将其推送到上一个标签
    4. 下方
    5. 段落文字重叠


我需要什么

我需要一种方法,以便在每个标签点击(相同或其他标签)时,它将清除最后一个标签的所有先前内容(任何按钮/段落/图像类)。

另外,jQueryUI .selectable()的另一个问题是它允许一次选择多个标签,如果你将光标拖到我的JSFiddle中的标签上,你会看到我的意思,我只想要它能够最多选择一个


我的代码已细分

的jQuery

//Tab Content added in relation to tab number
if ($('#elements .elementOne').hasClass('ui-selected')) {
            // Tab 1
            $(".elementOneInfo").addClass("elementOneInfoImage");
            $(".elementOneInfo").append("<button><a href='/clans'>MORE<two</button>");
            $(".elementOneInfo").append("<p>"+textOne+"</p>");
        }
if ($('#elements .elementTwo').hasClass('ui-selected')) {
            //Tab 2 etc.
            $(".elementTwoInfo").addClass("elementTwoInfoImage");
            [...]

HTML

<div class="container elements-wrapper">
    <div class="col-md-12">
        <div class="row">

            <!-- Where new content is added -->

            <div class="col-md-7 elements-info">
                <div class="elementOneInfo"></div>
                <div class="elementTwoInfo"></div>
                <div class="elementThreeInfo"></div>
                <div class="elementFourInfo"></div>
            </div>

            <!-- Where tabs exist -->

            <div class="col-md-5 elements-titles-wrapper">
                <ul id="elements">
                    <li class="elementOne light ui-widget-content"><h3>Title1</h3></li>
                    <li class="elementTwo dark ui-widget-content"><h3>Title2</h3></li>
                    <li class="elementThree light ui-widget-content"><h3>Title3</h3></li>
                    <li class="elementFour dark ui-widget-content"><h3>Title4</h3></li>
                </ul>
            </div>
        </div>
    </div>
</div>

SCSS

/* Tabs */

.elements-titles-wrapper {
    padding:0;
    ul {
        list-style: none;
        padding-left: 0;
    }
    li > h3 {
        color: white;
        margin:0;
        padding: 15px 0 15px 15px;
    }
    .light {
        background-color: $aqua;
    }

    .dark {
        background-color: $dark_aqua;
    }

    .active {
        background-color: #75c5e0;
    }
    .ui-selected {
        background: $light_aqua;
    }
    .ui-selecting {
        background: $light_aqua;
    }
}


/* Tab Content Added */

.elements-info {
    padding:0;
    p {
        color:white;
        font-size:12px;
        background: rgba(0,0,0,.5);
        padding: 10px;
        position:absolute;
        bottom:0;
        margin-bottom: 0;
        @include font(Open-Sans);
    }
    button {
        background: $cream;
        border-radius:5px;
        margin: 5px 0 0 5px;
        a {
            color:$brown;
            @include font(Open-Sans);
            padding: 4px 8px;
            text-decoration: none;
        }
    }

    /* Random Background Images for testing */
    .elementOneInfoImage {
        background: url(http://wallpaperkick.com/wp-content/uploads/2015/02/gray-wallpaper.jpg) no-repeat center right;
        height:224px;
    }
    .elementTwoInfoImage {
        background: url(http://cdn.wonderfulengineering.com/wp-content/uploads/2014/09/red-wallpaper-4.jpg) no-repeat center right;
        height:224px;
    }
    .elementThreeInfoImage {
        background: url(http://cdn.wonderfulengineering.com/wp-content/uploads/2014/09/Green-Wallpaper-3.jpg) no-repeat center right;
        height:224px;
    }
    .elementFourInfoImage {
        background: url(http://walldiskpaper.com/wp-content/uploads/2014/11/Blue-Light-Wallpaper-Image-Picture.png) no-repeat center right;
        height:224px;
    }
}

感谢阅读,我希望有人能够告诉我如何实现这一目标,我已经能够添加我用一个标签发送的视觉图像的外观和感觉,但是一旦我添加将标签切换到混合中它只是没有用。

1 个答案:

答案 0 :(得分:0)

尝试

$(document).ready(function () {
    var content = {
        "server-one": "Aenean eu leo quam."
          + " Pellentesque ornare sem lacinia quam venenatis vestibulum."
          + " Donec id elit non mi porta gravida at eget metus."
          + " <button><a href='/clans'>MORE<two</button>",
        "server-two": "Nulla vitae elit libero, a pharetra augue."
          + " Etiam porta sem malesuada magna mollis euismod."
          + " <button><a href='/clans'>MORE<two</button>",
        "server-three": "Cum sociis natoque penatibus et magnis "
          + "dis parturient montes, nascetur ridiculus mus."
          + " Nullam quis risus eget urna mollis ornare vel eu leo."
          + " <button><a href='/clans'>MORE<two</button>",
        "server-four": "Donec id elit non mi porta gravida at eget metus."
          + " Aenean eu leo quam."
          + " Pellentesque ornare sem lacinia quam venenatis vestibulum."
          + " <button><a href='/clans'>MORE<two</button>"
    };

    $('#server-titles').selectable({
        selected: function (event, ui) {
            var sel = $(ui.selected).attr("class").split(" ")[0];
            var elem = $(".row [class^=" + sel.concat("-info") + "]");
            elem.html(content[sel]).show(0).siblings().hide(0);
             
        }
    });
});
div[class$=image] {
    display:none;
}

/* Server Tabs */
/* line 423, ../sass/home.scss */
.server-titles-wrapper {
  padding: 0;
}
/* line 425, ../sass/home.scss */
.server-titles-wrapper ul {
  list-style: none;
  padding-left: 0;
}
/* line 429, ../sass/home.scss */
.server-titles-wrapper .server > h3 {
  color: white;
  margin: 0;
  padding: 15px 0 15px 15px;
}
/* line 434, ../sass/home.scss */
.server-titles-wrapper .light {
  background-color: #6bb3cc;
}
/* line 438, ../sass/home.scss */
.server-titles-wrapper .dark {
  background-color: #61a4ba;
}
/* line 442, ../sass/home.scss */
.server-titles-wrapper .active {
  background-color: #75c5e0;
}
/* line 445, ../sass/home.scss */
.server-titles-wrapper .ui-selected {
  background: #75c5e0;
}
/* line 448, ../sass/home.scss */
.server-titles-wrapper .ui-selecting {
  background: #75c5e0;
}

/* line 453, ../sass/home.scss */
.server-info {
  padding: 0;
}
/* line 455, ../sass/home.scss */
.server-info p {
  color: white;
  font-size: 12px;
  background: rgba(0, 0, 0, 0.5);
  padding: 10px;
  position: absolute;
  bottom: 0;
  margin-bottom: 0;
  font-family: Open-Sans, Trebuchet MS, Serif;
}
/* line 465, ../sass/home.scss */
.server-info button {
  background: #f4dab9;
  border-radius: 5px;
  margin: 10px 0 0 10px;
}
/* line 469, ../sass/home.scss */
.server-info button a {
  color: #946d3b;
  font-family: Open-Sans, Trebuchet MS, Serif;
  padding: 4px 8px;
  text-decoration: none;
}
/* line 476, ../sass/home.scss */
.server-info .server-one-info-image {
  background: url(../img/clans.jpg) no-repeat center right;
  height: 224px;
}
/* line 480, ../sass/home.scss */
.server-info .server-two-info-image {
  background: url(../img/test.jpg) no-repeat center right;
  height: 224px;
}

.server-info .server-one-info-image {
    background: url(http://wallpaperkick.com/wp-content/uploads/2015/02/gray-wallpaper.jpg) no-repeat center right;
    height:224px;
}
.server-info .server-two-info-image {
    background: url(http://cdn.wonderfulengineering.com/wp-content/uploads/2014/09/red-wallpaper-4.jpg) no-repeat center right;
    height:224px;
}
.server-info .server-three-info-image {
    background: url(http://cdn.wonderfulengineering.com/wp-content/uploads/2014/09/Green-Wallpaper-3.jpg) no-repeat center right;
    height:224px;
}
.server-info .server-four-info-image {
    background: url(http://walldiskpaper.com/wp-content/uploads/2014/11/Blue-Light-Wallpaper-Image-Picture.png) no-repeat center right;
    height:224px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js">
</script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container server-wrapper">
    <div class="col-md-12">
        <div class="row">
            <div class="col-md-7 server-info">
                <div class="server-one-info server-one-info-image"></div>
                <div class="server-two-info server-two-info-image"></div>
                <div class="server-three-info server-three-info-image"></div>
                <div class="server-four-info server-four-info-image"></div>
            </div>
            <div class="col-md-5 server-titles-wrapper">
                <div id="display"></div>
                <ul id="server-titles">
                    <li class="server-one light server ui-widget-content">
                        <h3>Title1</h3>
                    </li>
                    <li class="server-two dark server ui-widget-content">
                        <h3>Title2</h3>
                    </li>
                    <li class="server-three light server ui-widget-content">
                        <h3>Title3</h3>
                    </li>
                    <li class="server-four dark server ui-widget-content">
                        <h3>Title4</h3>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

jsfiddle http://jsfiddle.net/p5gsby49/12/