javascript - jQuery check if element has a class -
i trying check see if html element has class , if so, run function jquery. code works if element doesn't have multiple classes. believe need use .hasclass()
method, couldn't figure out.
var pageclass = $("body").attr('class'); switch (pageclass) { case ("page1"): $('h1').html('heading1'); break; case ("page2"): $('h1').html('heading2'); break; default: $('h1').html('default'); }
fiddle: https://jsfiddle.net/9o70dbzz/2/
this can solved selectors:
$('h1').html('default'); $('body.page1 h1').html('heading1'); $('body.page2 h1').html('heading2');
Comments
Post a Comment