简体   繁体   English

更改的文字 <h1> 如果屏幕缩小

[英]Change text of <h1> if screen is reduced

I have a code trying to change the text of an h1 if the screen is reduced in size, here's the actual code I have, but is not working. 如果屏幕尺寸减小,我有一个代码试图更改h1的文本,这是我的实际代码,但无法正常工作。 Someone could help me to correct it and to understand it? 有人可以帮助我更正并理解它吗? thanks! 谢谢!

HTML HTML

<h1 id="text1">Test</h1>

JAVASCRIPT JAVASCRIPT

if($(window).width() <= 500){
$('#text1').Text('testing');  
}else{
$('#text1').Text('buuu');
}

You need to work along with window resize event. 您需要处理window调整大小事件。 Other option is using CSS pseudo code. 另一种选择是使用CSS伪代码。

 @media (min-width: 500px) { #text1:after { content: "foo"; } } @media (max-width: 499px) { #text1:after { content: "bar"; } } 
 <h1 id="text1"></h1> 

Edited As per Niet the Dark Absol suggestion. 根据Niet the Dark Absol建议进行编辑

 @media (min-width: 500px) { #text1 > .tiny { display: none; } } @media (max-width: 499px) { #text1 > .big { display: none; } } 
 <h1 id="text1"> <span class="big">Bigger Content</span> <span class="tiny">Smaller Content</span> </h1> 

Maybe CSS could help you here: 也许CSS可以在这里为您提供帮助:


use data- attributes to validate HTML5 and mediaqueries to show its value. 使用data- attribute属性验证HTML5,并使用mediaqueries显示其价值。 Old text-indent method to erase at screen text hold in the tag itself. 标记文本本身保留在屏幕上的旧文本缩进方法。

You can use it without id or class for entire site, but data- attribute will need to be there and filled, else a class to set wherever needed, or id for a single use. 您可以在没有idclass情况下将其用于整个站点,但是data- attribute必须存在并填充,否则,可以在需要的地方设置class ,或仅使用id

  @media all and (max-width:500px) { h1 { text-indent:-9999px; } h1:before { content:attr(data-text); text-indent:0; float:left; } } 
 <h1 data-text="short text">My long text stands here</h1> 

You have to catch the window's resizing events. 您必须捕获窗口的调整大小事件。 Pretty easy to do it with jquery . jquery很容易做到。 Try this: 尝试这个:

$(window).resize(function(){
    if ($(window).width() <= 500){
        $('#text1').Text('testing');  
    }
    else {
        $('#text1').Text('buuu');
    }
});

http://jsbin.com/puzanajepe/1/edit?html,js,output http://jsbin.com/puzanajepe/1/edit?html,js,output

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

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