简体   繁体   English

根据屏幕宽度切换“ a href”文本

[英]Toggling text of “a href” based on screen width

Using jQuery/jQueryMobile, I have a link as follows: 使用jQuery / jQueryMobile,我有如下链接:

<a href="index.html" id="HomeLink" data-role="button" data-mini="true" data-icon="home" data-iconpos="top" >Home</a>

I am trying to test various screen sizes, and if the screen width is less than 300px, I want to change: 我正在尝试测试各种屏幕尺寸,如果屏幕宽度小于300px,我想更改:

data-iconpos="top"

to

data-iconpos="notext"

so I only get the icon. 所以我只得到图标。 I have tried to do it with JavaScript: 我试图用JavaScript做到这一点:

var hl = document.querySelector('#HomeLink');
if ($(window).width() < 300) {
    hl.setAttribute("data-iconpos", "notext");
} else {
    hl.setAttribute("data-iconpos", "top");
}

But it won't work. 但这是行不通的。

Question: can it be done in CSS instead. 问题:可以用CSS代替吗?

If not: how can it be done in JavaScript? 如果没有:如何用JavaScript完成?

You can't really set a data attribute with CSS as far as I know, but since you're already using jQuery, why not try it all the way : 据我所知,您实际上无法使用CSS设置数据属性,但是由于您已经在使用jQuery,所以为什么不尝试一下:

$('#HomeLink').data('iconpos', ($(window).width() < 300 ? 'notext' : 'top') );

Remember to wrap that in document ready! 请记住将其包装在文档中!

You can do this way altering your code: 您可以通过这种方式更改代码:

    var hl = $('#HomeLink');
    if ($(window).width() < 300) {
        hl.data("iconpos", "notext");
    } else {
        hl.data("iconpos", "top");
    }

with .attr() : .attr()

    var hl = $('#HomeLink');
    if ($(window).width() < 300) {
        hl.attr("data-iconpos", "notext");
    } else {
        hl.data("data-iconpos", "top");
    }

Try to wrap the code in window resize event, eg: 尝试将代码包装在窗口调整大小事件中,例如:

$(window).resize(function () {
check();
})
$(function () {
check();
})

function check() {
if ($(window).width() < 300) {
    $('#HomeLink').attr('data-iconpos', 'notext');
} else {
    $('#HomeLink').attr('data-iconpos', 'top');
}
}

I would like to suggest using a different approach. 我建议使用其他方法。

The data-iconpos="top" looks a bit out of place to me here. 在这里, data-iconpos="top"在我看来有点data-iconpos="top" I have a feeling (perhaps I'm wrong) that you're attempting to inline your styling into the HTML. 我有一种感觉(也许是错的),您正在尝试将样式内联到HTML中。

Why not try media queries? 为什么不尝试媒体查询?

@media screen and (max-width: 300px) {
    #HomeLink {
        /* styling for HomeLink when screen width is less than 300px */
    }
}

This is a CSS-only solution. 这是仅CSS的解决方案。 It works if the user decides to resize the screen after the page has loaded. 如果用户决定在页面加载后调整屏幕大小,则此方法有效。 Try resizing the "Result" frame in this jsfiddle and notice the color of the link changing. 尝试调整此jsfiddle中 “结果”框架的大小,并注意链接颜色的变化。

Suggested reading: 建议阅读:

And here are the docs on media queries: http://www.w3.org/TR/css3-mediaqueries/ 以下是有关媒体查询的文档: http : //www.w3.org/TR/css3-mediaqueries/

Warning : mind ahem, IE below 9, ahem... 警告 :请记住,IE低于9,...

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

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