2017-10-08 63 views
-1

如何製作div的一部分對於使用jQuery的屏幕閱讀器不可見? 我試圖在某些文本上實現readmore readless功能,但我面臨屏幕閱讀器的問題,但我必須在這裏允許accessibilty。 這裏是我的代碼,我將展示基於行數我的DIV的一部分:屏幕閱讀器的易用性

$(document).ready(function() { 
var divHeight,l=3; 
$('.comment').each(function() { 
var $this = $(this),divHeight; 
var $content = $this.find(".text-content"), 
    lineHeight= $content.css('line-height'); 
    divHeight= ((parseInt(l)+3)*parseInt(lineHeight))+"px"; 
    if($content.hasClass("short-text")){ 
     $content.css("height","auto"); 
    } 
    else{ 
    $content.addClass("short-text"); 
    $content.css("overflow","hidden"); 
    $content.css("height",divHeight); 
    $content.on("focus",function(){ 
     $(this).attr('aria-hidden', 'false'); 
    }) 
    } 
    html='<p class="'+'para'+'"><a href="#" class="morelink morelinkStyle">' + 'more' + '</a></p></span>'; 
    $this.append(html); 
}); 
$(".morelink").click(function(){ 
    var $this = $(this), 
    $elem=$this.parents().find(".text-content"); 
    if ($elem.hasClass("short-text")) { 
     $elem.removeClass("short-text"); 
     $this.html('less'); 
     divHeight = $elem.height(); 
     $elem.css("height","auto"); 
    } else { 
     $elem.addClass("short-text"); 
     $elem.on("focus",function(){ 
      console.log($(this)); 
      $(this).css("border","2px solid red"); 
     $(this).attr('aria-hidden', 'false'); 
     }) 
     $this.html('more'); 
     $elem.css("height",divHeight); 
     $elem.css("overflow","hidden"); 
    } 
    return false; 
    }); 

HTML之中:

<div class="comment"> 
<article class="text-content"> 
    <h3>Bedspreads</h3> 
<p>Bedspreads add an elegant, ornamental look to any <a href="/store/category/bedding/10504/">bedroom</a>. They may just sit atop your bed but the right spread can pull an entire room together and create a central focal point to your bedroom. That's why Bed Bath &amp; Beyond offers a large array of bedspreads and comforters to help you find exactly what you are looking for. Wow any visitor or guest with a beautiful,</p> <p>1st para -vibrant floral pattern that brightens up any room, create a bold look with solid colors and modern styling or bring in a traditional design with classic chenille patterns. Turn your room into a viewing experience today with our ever-growing selection.Ut enim ad minim veniam, 
</p> 
<p>2nd para-vibrant floral pattern that brightens up any room, create a bold look with solid colors and modern styling or bring in a traditional design with classic chenille patterns. Turn your room into a viewing experience today with our ever-growing selection.Ut enim ad minim veniam,</p> 
</article> 
</div> 
+0

在這裏,我已採取線爲3最初在可變升 – user3771215

+0

我已經給定的行高度的元件以及 – user3771215

回答

0

編輯div的高度&溢出屬性將不會隱藏來自屏幕閱讀器的內容。您需要在短文本類中使用display:none。 (另外,在你的CSS中編寫類更容易,只需使用jquery添加/刪除類名)。

0

幾個月前,我負責解決實施「Read More」/「Read Less」鏈接的同一問題,同時保持WCAG 2.0的可訪問性。

如果你不反對使用Bootstrap和jQuery,我有一個很好的解決方案。引導程序collapse類使它成爲可能。

https://jsfiddle.net/rmatnwrm/