jquery - Alternative to multiple if else statements in javascript -
my question similar reduce multiple if else statements
i have multiple if else statements , i'd use jquery each function make code more efficient, can't figure out how it.
i'm running jquery in wordpress believe runs in noconflict mode, can't lot of more (what consider) advanced topics give examples work me, can't understand right function syntax use.
if , explain how me amazing. here code:
var $h6p = $("h6 + p"); var $h5p = $("h5 + p"); var $h4p = $("h4 + p"); var $h3p = $("h3 + p"); var $h2p = $("h2 + p"); var $h1p = $("h1 + p"); var $fullercolor_bg = "rgba(240,234,222,0.9)"; if($h1p.mouseisover()) { $h1p.prev().css("background-color", $fullercolor_bg); } else { $h1p.prev().css("background-color", ""); } if($h2p.mouseisover()) { $h2p.prev().css("background-color", $fullercolor_bg); } else { $h2p.prev().css("background-color", ""); } if($h3p.mouseisover()) { $h3p.prev().css("background-color", $fullercolor_bg); } else { $h3p.prev().css("background-color", ""); } if($h4p.mouseisover()) { $h4p.prev().css("background-color", $fullercolor_bg); } else { $h4p.prev().css("background-color", ""); } if($h5p.mouseisover()) { $h5p.prev().css("background-color", $fullercolor_bg); } else { $h5p.prev().css("background-color", ""); } if($h6p.mouseisover()) { $h6p.prev().css("background-color", $fullercolor_bg); } else { $h6p.prev().css("background-color", ""); }
(if css had previous adjacent siblings selector on moon @ point.)
edit: far, 1 thing should have mentioned empty setting of else statement deliberate. have used css target sibling selector , background-color set in that, need set. not transparent.
you use array:
var $hp = ["h6 + p", "h5 + p", "h4 + p", "h3 + p", "h2 + p", "h1 + p"], $fullercolor_bg = "rgba(240,234,222,0.9)"; $hp.foreach(function(v) { if($(v).mouseisover()) { $(v).prev().css({ backgroundcolor: $fullercolor_bg }); } else { $(v).prev().css({ backgroundcolor: "transparent" }); } });
in case think it's simpler use multiple css selectors within variable. may or may not work depending on implementation of mouseisover
:
var $hp = $("h6 + p, h5 + p, h4 + p, h3 + p, h2 + p, h1 + p"), $fullercolor_bg = "rgba(240,234,222,0.9)"; if($hp.mouseisover()) { $hp.prev().css({ backgroundcolor: $fullercolor_bg }); } else { $hp.prev().css({ backgroundcolor: "transparent" }); }
Comments
Post a Comment