简体   繁体   中英

Using Javascript to ONLY hide a link/txt/div

I have a site with a Javascript image viewer to enlarge my pics.

Therefore if you browse with JS enabled you dont have to see the link to another page witch shows the pics enlarged.

if JS is disabled you wont see my function to enlarge pics with javascript, but will see my link to the enlarged page.

I need a code to just hide the link. i keep finding TOGGLE switches.

Instead of hiding the link, just put it in noscript tags:

<noscript>
  <a href="/url">This link only shows up for those without javascript enabled</a>
</noscript>

usually what you need to do is to put your image inside a link that has both href and onclick attributes

<a href="www.mysite.com/link_to_my_big_image.jpg" onclick="showBigImage();return false;">
    <img src="my_small_image.jpg" alt="click me"/>
</a>

if JS is disabled, the onclick wont work, and if js is enabled the link wont redirect the page

Good Luck

what i do in those cases is setting a class named nojs on the body or the HTML. Then i remove it with javascript. Then you can use the following CSS to cover both cases:

.js .yourelement {
   display: none;
}

.nojs .yourelement {
   display: block;
}

With jQuery you can do:

$(function(){ $("body").addClass("js").removeClass("nojs"); });

Its how its done in the HTML5 boilerplate as well.

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