简体   繁体   English

根据屏幕宽度隐藏链接标题属性-不会取消隐藏

[英]Hide link title attribute based on screen width – won't unhide

I've made an attempt here based on some things I have gleaned, but this is just plain removing it altogether; 我已经根据收集到的一些信息进行了尝试,但这只是完全删除它; the title doesn't return back at <= 768px 标题不会以<= 768px返回

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

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

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

    });
}
</script>

See http://jsfiddle.net/2nHxV/ 参见http://jsfiddle.net/2nHxV/

So put it back? 那么放回去吗?

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'));
    });
}

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

You also might want to put this logic inside a $(window).on('resize', ...); 您可能还想将此逻辑放在$(window).on('resize', ...); handler since it'll only be executed once on page load as it stands currently. 处理程序,因为它在当前页面加载时仅会执行一次。

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');
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM