简体   繁体   中英

Change text on screen size

How to change text in a div if the screen is less than..?

<a href="#">View full profile</a>
if(screen <= 768) {
   <a href="#">View</a>
}

Sorry I'm new to Javascript. I've seen similar questions around but any of them has a correct answer.

I know I could use css to achieve this but I don't want to repeat the html code just to hide/show elements.

Html:

<a id="viewLink" href="#">View full profile</a>

Javascript:

// Get the window width
if (window.innerWidth < 768) {

    // If less than 768
    document.getElementById('viewLink').innerText = 'View';
    // Change the link text to View.
}

Script

if (window.innerWidth < 768) {
document.getElementById('viewLink').innerText = 'View'; 
}

and HTML

<a id="viewLink" href="#">View full profile</a>

Try using media queries like here:

//css
@media screen and (max-width: 768px) {
    .tablet {
        display: none; 
    } 
}
@media screen and (min-width: 769px) {
    .phone { 
        display: none;
    }
}

//html
<a href="#" class='tablet'>View full profile</a>
<a href="#" class='phone'>View</a>

You can try this method...

 if (window.innerWidth < 768) { $('#hide').hide(); } 
 <a href="#">View <span id="hide">full profile</span></a> 

  <a id="viewText" href="#">View full profile</a>
    <script type=text/javascript>
    $(document).ready(function () {

        if (screen.width < 768) {
            $("#viewText").show();
        }
        else {

            $("#viewText").hide();
        }

    });
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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