html5 - jQuery onclick don't work with a href and img -
i want display divs when clicking on link id.
jquery:
$('#tab-bar a').on('click', function(e){ e.preventdefault(); var nextpage = $(e.target.hash); page(nextpage); $("#pages .current").removeclass("current"); nextpage.addclass("current"); }); html:
<div id="pages"> <div id="mainpage" class="current"> <ul id="tab-bar"> <li> <a href="#mimik"><img src="img/mimik.jpg" alt="mimik"></img></a><br> </li> </ul> </div> <div id="mimik"> <h2 style="color:red">mimik</h2> </div> when use text instead of image in works wish. h2 displayed. image in "link" displays blank page. how can solve problem?
best regards simon
that because when have image inside anchor event target img element when click on , doesn't have hash property. try using closest method find closest a element target element.
$('#tab-bar a').on('click', function(e){ e.preventdefault(); var nextpage = $($(e.target).closest('a')[0].hash); page(nextpage); $("#pages .current").removeclass("current"); nextpage.addclass("current"); }); alternatively can use this.hash this point element on event attached.
$('#tab-bar a').on('click', function(e){ e.preventdefault(); var nextpage = $(this.hash); page(nextpage); $("#pages .current").removeclass("current"); nextpage.addclass("current"); });
Comments
Post a Comment