简体   繁体   English

当屏幕宽度小于不工作的数量时更改文本

[英]Change text when screen width is less than amount not working

I'm trying to change the text on this website when the screen size is less than 650, but it's not working.当屏幕尺寸小于 650 时,我试图更改此网站上的文本,但它不起作用。 I've tried the answers from Do something if screen width is less than 960 px and Resizing images when screen is less than 1024 pixels width , which did not work.我已经尝试了Do something if screen width is less than 960 pxResizing images when screen is less than 1024 pixel width的答案,但没有用。 Here is the link to the replit, and the code snippet is also below.是replit的链接,代码片段也在下面。

if ($(window).width() < 650) { // I've also tried (window).width() and window.width
var words = $('.words');
words.text("Hello! This is the new text!");
}
else { 
}


<div class="intro">
  <div>
    <h1 class="ele">Hey there!</h1>
    <p class="ele words">Hi! This is the old text."</p>
  </div>
  <img class="ele me" src="me.png" alt="A picture of myself.">
</div>

I tried your code on CodePen and it seems to work fine, maybe your script is being loaded in the wrong place or the elements you're trying to change where not created yet, maybe some more code could help see the example我在 CodePen 上尝试了您的代码,它似乎工作正常,也许您的脚本加载到了错误的位置,或者您尝试更改的元素尚未创建,也许更多的代码可以帮助查看示例

<div class="intro">
  <div>
    <h1 class="ele">Hey there!</h1>
    <p class="ele words">Hi! This is the old text."</p>
  </div>
  <img class="ele me" src="me.png" alt="A picture of myself.">
</div>
$(window).resize(function() {
  if ($(window).width() < 650) {
    var words = $('.words');
    words.text("Hello! This is the new text!");
    }
    else { 
      var words = $('.words');
    words.text("Hello! This is the old text.");
  }
})

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

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