2017-07-10 120 views
1

我有查看器的對象與切換器的列表視圖和網格視圖。我還做了一些陳述,說當用戶選擇網格視圖並且在描述中超過35個字符時,用戶只能看到35個字符,但是當用戶回到列表視圖時,我希望它回到完整描述。你可以有任何解決方案嗎?jQuery單擊後刪除類

$(document).ready(function() { 
 
    $("a.switcher").bind("click", function(event) { 
 
    event.preventDefault(); 
 

 
    var mainId = $(this).attr("id"); 
 
    var classNames = $(this).attr('class').split(' '); 
 
    var companyView = $("ul#company"); 
 
    var linkList = $('span#link-list'); 
 
    var gridDescription = $('span.description'); 
 
    var listDescription = $('span.meta'); 
 

 
    if (mainId == "gridview") { 
 
     $(this).addClass("active"); 
 
     $("#listview").removeClass("active"); 
 

 
     companyView.removeClass("list"); 
 
     companyView.addClass("grid col-xs-12"); 
 
     linkList.addClass("links-position"); 
 

 
     gridDescription.addClass('.restrict'); 
 
     var txt = $('.description').text(); 
 

 
     if (txt.length > 35 && mainId == "gridview") { 
 
     $('.description').html(txt.substring(0, 35) + ' <a class="morelink" href="">[...]</a>'); 
 
     } 
 
     
 
     $(".morelink").click(function(event) { 
 
     event.preventDefault(); 
 

 
     $("#gridview").removeClass("active"); 
 
     $("#listview").addClass("active"); 
 

 
     companyView.removeClass("grid col-xs-12"); 
 
     companyView.addClass("list"); 
 
     linkList.removeClass("links-position"); 
 
     }); 
 
    } else if (mainId == "listview") { 
 
     $(this).addClass("active"); 
 
     $("#gridview").removeClass("active"); 
 
     companyView.removeClass("grid col-xs-12"); 
 
     companyView.addClass("list"); 
 
     linkList.removeClass("links-position"); 
 
    } 
 
    }); 
 
});
* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
body { 
 
    color: #333; 
 
    padding-bottom: 25px; 
 
} 
 

 
img { 
 
    border: 0; 
 
} 
 

 
p { 
 
    font-size: 1.2em; 
 
    line-height: 1.3em; 
 
    margin-bottom: 10px; 
 
} 
 

 
#wrap { 
 
    margin: 0 auto; 
 
    border-radius: 7px; 
 
} 
 

 
#wrap header { 
 
    border-bottom: 1px solid white; 
 
    margin-bottom: 35px; 
 
    padding-top: 20px; 
 
    position: relative; 
 
} 
 

 
#wrap header .list-style-buttons { 
 
    color: red; 
 
    position: absolute; 
 
    right: 0; 
 
} 
 

 
.switcher { 
 
    color: red; 
 
} 
 

 
.switcher:active { 
 
    color: #da1d15; 
 
} 
 

 
.switcher:visited { 
 
    color: #da1d15; 
 
} 
 

 
ul.list { 
 
    margin-bottom: 20px; 
 
    list-style: none; 
 
    width: 100%; 
 
} 
 

 
ul.list li { 
 
    margin-bottom: 20px; 
 
    display: block; 
 
} 
 

 
ul.list li section.left { 
 
    margin: 10px; 
 
    display: block; 
 
    float: left; 
 
    position: relative; 
 
    padding-left: 20px; 
 
} 
 

 
ul.list li .meta { 
 
    display: block; 
 
} 
 

 
ul.list li .links { 
 
    display: inline; 
 
} 
 

 
ul.list li .secondOffer { 
 
    margin-left: 20px; 
 
} 
 

 
ul.list li section.left img.thumb { 
 
    margin: 10px; 
 
    height: 150px; 
 
    width: 300px; 
 
    float: right; 
 
} 
 

 
ul.list li section.left h3 { 
 
    text-align: left; 
 
    font-weight: bold; 
 
    text-transform: uppercase; 
 
    font-size: 1.4em; 
 
} 
 

 
ul.list section.left { 
 
    padding: 20px; 
 
} 
 

 
ul.grid { 
 
    list-style: none; 
 
    margin: 0 auto; 
 
} 
 

 
ul.grid li section.left img.thumb { 
 
    height: 150px; 
 
    width: 300px; 
 
    position: relative; 
 
} 
 

 
ul.grid li { 
 
    display: block; 
 
    float: left; 
 
    width: 270px; 
 
    height: 150px; 
 
    padding: 5px 30px; 
 
    margin-bottom: 20px; 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
} 
 

 
ul.grid li section.left { 
 
    padding: 10px; 
 
} 
 

 
ul.grid section.left { 
 
    position: relative; 
 
    height: 400px; 
 
} 
 

 
.links-position { 
 
    position: absolute; 
 
    bottom: 0; 
 
} 
 

 
ul.grid li.clearfix { 
 
    margin-bottom: 300px; 
 
} 
 

 
ul.grid li section.left h3 { 
 
    position: relative; 
 
    font-weight: bold; 
 
    text-transform: uppercase; 
 
    font-size: 1.2em; 
 
    line-height: 1.5em; 
 
} 
 

 
ul.grid li .meta { 
 
    display: block; 
 
} 
 

 
ul.grid li .address { 
 
    position: relative; 
 
} 
 

 
ul.grid li .description { 
 
    position: absolute; 
 
} 
 

 
ul.grid li .links { 
 
    padding-left: 20px; 
 
} 
 

 
ul.grid .clearfix { 
 
    padding: 10px; 
 
} 
 

 
.clearfix {} 
 

 
section.left { 
 
    -webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 
 
    -moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 
 
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2); 
 
} 
 

 
section.left:hover { 
 
    -webkit-box-shadow: 0 0 10px rgba(207, 168, 168, 1); 
 
    -moz-box-shadow: 0 0 10px rgba(207, 168, 168, 1); 
 
    box-shadow: 0 0 15px rgba(207, 168, 168, 1); 
 
} 
 

 
a:link { 
 
    color: red; 
 
    text-decoration: none; 
 
} 
 

 
a.links:visited { 
 
    color: red; 
 
    text-decoration: none; 
 
} 
 

 
a.morelink { 
 
    text-decoration: none; 
 
    outline: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrap" class="col-xs-12"> 
 
    <header> 
 
    <span class="list-style-buttons"> 
 
     <a href="#" id="listview" class="switcher"><i class="fa fa-2x fa-th-list"></i></a> 
 
     <a href="#" id="gridview" class="switcher active"><i class="fa fa-2x fa-th"></i></a> 
 
    </span> 
 
    </header> 
 
    <ul id="company" class="list clearfix"> 
 
    <li class="clearfix alt"> 
 
     <section class="left"> 
 
     <img src="" alt="company image" class="thumb"> 
 
     <a href=""> 
 
      <h3>Name</h3> 
 
     </a> 
 
     <span class="meta address"><strong>Address: </strong></span> 
 
     <span class="meta description"> 
 
      DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
     </span> 
 
     <span id="link-list"> 
 
      <a href="" class="links"> 
 
      <i class="fa fa-id-card-o"></i> 
 
      Users: 
 
      </a> 
 
      <a href="" class="links secondOffer"> 
 
      <i class="fa fa-database"></i> 
 
      Offers: 
 
      </a> 
 
     </span> 
 
     </section> 
 
    </li> 
 
    </ul> 
 
</div>

+1

您必須將原始「描述」存儲在某處以進行回滾。 –

+0

但你有什麼想法如何找到? –

+0

康拉德,檢查我的答案,讓我知道你是否有問題。 – SeanKendle

回答

0

您可以存儲jQuery的data存儲裏面很多東西(這與HTML5 data屬性一致,但不直接連接)。

$(document).ready(function() { 
 
     //`bind` is deprecated, use `on`: 
 
     $("a.switcher").on("click", function (event) { 
 
      event.preventDefault(); 
 

 
      var mainId = $(this).attr("id"); 
 
      var classNames = $(this).attr('class').split(' '); 
 
      var companyView = $("ul#company"); 
 
      var linkList = $('span#link-list'); 
 
      var gridDescription = $('span.description'); 
 
      var listDescription = $('span.meta'); 
 

 
      if (mainId == "gridview") { 
 

 
       $(this).addClass("active"); 
 

 
       $("#listview").removeClass("active"); 
 

 
       companyView.removeClass("list"); 
 
       companyView.addClass("grid col-xs-12"); 
 
       linkList.addClass("links-position"); 
 

 
       gridDescription.addClass('.restrict'); 
 
       
 
       //store local description: 
 
       var $description = $(".description"); 
 

 
       $.each($description, function(i, e){ 
 
\t \t \t \t \t var $thisDesc = $(this); 
 
\t \t \t \t \t var txt = $thisDesc.text(); 
 
\t \t \t \t \t $thisDesc.data("full-desc", txt); 
 
\t \t 
 
\t \t \t \t \t if (txt.length > 35 && mainId == "gridview") {      
 
\t \t \t \t \t \t $thisDesc.html(txt.substring(0, 35) + ' <a class="morelink" href="">[...]</a>'); 
 
\t \t \t \t \t } 
 
\t \t \t \t }); 
 

 
       //the .morelink a tag is being generated, you were trying to assign a listener *to that link*, but it doesn't exist yet, instead add a listener to a parent element that *will exist* on page load: 
 
       $("#company").on("click", ".morelink", function (event) { 
 
        event.preventDefault(); 
 
        
 
        $("#gridview").removeClass("active"); 
 
        $("#listview").addClass("active"); 
 
        
 
        //is this where you want to restore the full description? Whenever you want, you can then pull the full description from the data-full-desc field, like this: 
 
        var $description = $(this).parents("li").find(".description"); 
 
        $description.html($description.data("full-desc")); 
 
        
 
        companyView.removeClass("grid col-xs-12"); 
 
        companyView.addClass("list"); 
 
        linkList.removeClass("links-position"); 
 
       }); 
 

 

 
      } else if (mainId == "listview") { 
 

 
       $(this).addClass("active"); 
 

 

 
       $("#gridview").removeClass("active"); 
 

 
       companyView.removeClass("grid col-xs-12"); 
 
       companyView.addClass("list"); 
 
       linkList.removeClass("links-position"); 
 

 
       //restore descriptions: 
 
       $.each($(".description"), function(i, e){ 
 
        if(!!$(this).data("full-desc")) 
 
    \t \t \t \t \t \t  $(this).html($(this).data("full-desc")); 
 
\t \t \t \t }); 
 
      } 
 

 
     }); 
 
    });
* { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
body { 
 
    color: #333; 
 
    padding-bottom: 25px; 
 
} 
 

 
img { 
 
    border: 0; 
 
} 
 

 
p { 
 
    font-size: 1.2em; 
 
    line-height: 1.3em; 
 
    margin-bottom: 10px; 
 
} 
 

 

 

 
#wrap { 
 
    margin: 0 auto; 
 
    border-radius: 7px; 
 
} 
 

 
#wrap header { 
 
    border-bottom: 1px solid white; 
 
    margin-bottom: 35px; 
 
    padding-top: 20px; 
 
    position: relative; 
 
} 
 

 
#wrap header .list-style-buttons { 
 
    color: red; 
 
    position: absolute; 
 
    right: 0; 
 
} 
 

 
.switcher { 
 
    color: red; 
 
} 
 

 
.switcher:active { 
 
    color: #da1d15; 
 
} 
 

 
.switcher:visited { 
 
    color: #da1d15; 
 
} 
 

 

 
ul.list { 
 
    margin-bottom: 20px; 
 
    list-style: none; 
 
    width: 100%; 
 
} 
 

 
ul.list li { 
 
    margin-bottom: 20px; 
 
    display: block; 
 
} 
 

 
ul.list li section.left { 
 
    margin: 10px; 
 
    display: block; 
 
    float: left; 
 
    position: relative; 
 
    padding-left: 20px; 
 
} 
 

 
ul.list li .meta { 
 
    display: block; 
 

 
} 
 

 
ul.list li .links { 
 
    display: inline; 
 
} 
 

 
ul.list li .secondOffer { 
 
    margin-left: 20px; 
 
} 
 

 
ul.list li section.left img.thumb { 
 
    margin: 10px; 
 
    height: 150px; 
 
    width: 300px; 
 
    float: right; 
 
} 
 

 
ul.list li section.left h3 { 
 
    text-align: left; 
 
    font-weight: bold; 
 
    text-transform: uppercase; 
 
    font-size: 1.4em; 
 
} 
 

 
ul.list section.left { 
 
    padding: 20px; 
 
} 
 

 

 
ul.grid { 
 
    list-style: none; 
 
    margin: 0 auto; 
 
} 
 

 
ul.grid li section.left img.thumb { 
 
    height: 150px; 
 
    width: 300px; 
 
    position: relative; 
 
} 
 

 
ul.grid li { 
 
    display: block; 
 
    float: left; 
 
    width: 270px; 
 
    height: 150px; 
 
    padding: 5px 30px; 
 
    margin-bottom: 20px; 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
} 
 

 
ul.grid li section.left { 
 
    padding: 10px; 
 
} 
 

 
ul.grid section.left { 
 
    position: relative; 
 
    height: 400px; 
 
} 
 

 
.links-position { 
 
    position: absolute; 
 
    bottom: 0; 
 
} 
 

 
ul.grid li.clearfix { 
 
    margin-bottom: 300px; 
 
} 
 

 
ul.grid li section.left h3 { 
 
    position: relative; 
 
    font-weight: bold; 
 
    text-transform: uppercase; 
 
    font-size: 1.2em; 
 
    line-height: 1.5em; 
 
} 
 

 
ul.grid li .meta { 
 
    display: block; 
 
} 
 

 
ul.grid li .address { 
 
    position: relative; 
 
} 
 

 
ul.grid li .description { 
 
    position: absolute; 
 
} 
 

 
ul.grid li .links { 
 
    padding-left: 20px; 
 
} 
 

 
ul.grid .clearfix { 
 
    padding: 10px; 
 
} 
 

 
.clearfix { 
 
} 
 

 
section.left { 
 
    -webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 
 
    -moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 
 
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2); 
 
} 
 

 
section.left:hover { 
 
    -webkit-box-shadow: 0 0 10px rgba(207, 168, 168, 1); 
 
    -moz-box-shadow: 0 0 10px rgba(207, 168, 168, 1); 
 
    box-shadow: 0 0 15px rgba(207, 168, 168, 1); 
 
} 
 

 

 
a:link { 
 
    color: red; 
 
    text-decoration: none; 
 
} 
 

 
a.links:visited { 
 
    color: red; 
 
    text-decoration: none; 
 
} 
 

 
a.morelink { 
 
    text-decoration: none; 
 
    outline: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css" /> 
 
<div id="wrap" class="col-xs-12"> 
 
    <header> 
 
      <span class="list-style-buttons"> 
 
       <a href="#" id="listview" class="switcher"><i class="fa fa-2x fa-th-list"></i></a> 
 
       <a href="#" id="gridview" class="switcher active"><i class="fa fa-2x fa-th"></i></a> 
 
      </span> 
 
    </header> 
 
    <ul id="company" class="list clearfix"> 
 

 
      <li class="clearfix alt"> 
 
       <section class="left"> 
 
        <img src="" alt="company image" class="thumb"> 
 
        <a href="#"> 
 
         <h3>Name</h3></a> 
 
        <span class="meta address"><strong>Address: </strong></span> 
 
        <span class="meta description">DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION DESCRIPTION 
 
        </span> 
 
        <span id="link-list"> 
 
        <a href="" 
 
         class="links"><i class="fa fa-id-card-o"></i> 
 
         Users: </a> 
 

 
        <a href="" class="links secondOffer"><i class="fa fa-database"></i> Offers: </a> 
 
       </span> 
 
       </section> 
 
      </li> 
 
    </ul> 
 
</div>

jQuery的數據功能:https://api.jquery.com/data/

的。數據()方法允許我們的方式,是從圓形安全的任何類型的數據附加到DOM 元件內存泄漏引用和因此 。

0

當你在網格視圖是夾不適合你的元素內部的任何文本你可以使用的CSS text-overflow: clip

選項還包括:

text-overflow: clip; 
text-overflow: ellipsis; 
text-overflow: "…"; 
text-overflow: fade; 
text-overflow: fade(10px); 
text-overflow: fade(5%); 

來源:https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow

0

如果我理解你的問題,你希望每個被點擊的按鈕時拖班級之間進行切換,所以我prpose使用.toggleClass()

+0

這不是OP之後的事情,他們想要顯示一個簡短的描述,然後在點擊「morelink」後恢復完整的描述,如果我沒有弄錯的話。 – SeanKendle