2014-11-21 55 views
0

我在此基礎上嘗試了一些我已經收集到的內容,但這只是簡單地將其完全刪除;標題不會在< = 768px根據屏幕寬度隱藏鏈接標題屬性 - 不會取消隱藏

<script> 
if($(window).width() > 767) { 

    $('[title]').each(function() { 

     var $this = $(this); 
     $this.data('title',$this.attr('title')); 
     $this.removeAttr('title'); 

    }); 
} 
</script> 

返回回見http://jsfiddle.net/2nHxV/

+0

的現場演示不包括寬度測試代碼... – Quentin 2014-11-21 23:45:29

+0

_「稱號,回不回在<= 768px」 _ - 和你期望通過自己神奇地出現......? – CBroe 2014-11-22 00:04:17

回答

1

所以把它放回去?

var $titles = []; 

if($(window).width()> 767) { 
    $('[title]').each(function() { 
     var $this = $(this); 
     $this.data('title',$this.attr('title')); 
     $this.removeAttr('title'); 

     $titles.push($this); 
    }); 
} else { 
    $.each($titles, function(index, $this) { 
     $this.attr('title',$this.data('title')); 
    }); 
} 

工作演示:http://jsfiddle.net/z3rr9d04/

您還可能希望把這個邏輯$(window).on('resize', ...);處理器中,因爲它只會進行一次在頁面加載當前標準的執行。

+0

這裏的兩個答案我喜歡這個緩存元素引用,並且只遍歷DOM一次。 – Maverick 2014-11-21 23:54:46

1
if ($(window).width() > 767) { 
    $('[title]').each(function() { 
     var $this = $(this); 
     $this.data('title',$this.attr('title')); 
     $this.removeAttr('title'); 
    }); 
} else { 
    // as in above `title` attribute removed and `data-title` added, so now you've 
    // to loop with data-title 
    $('[data-title]').each(function() { 
     var $this = $(this); 
     $this.data('title',$this.data('title')); 
     $this.removeAttr('data-title'); 
    }); 
}