jquery - Javascript function dosn't works when I work with templates -
i have html file in javascript code.
<div id="star_rating"> <a id="one">☆</a><a id="two">☆</a><a id="three">☆</a> </div> <script type="text/javascript"> $(document).on("ready", function() { $("#one").hover( function () { markhover("true", "false", "false", "false", "false"); },function () { markhover("false", "false", "false", "false", "false"); } ); $("#two").hover( function () { markhover("true", "false", "false", "false", "false"); },function () { markhover("false", "false", "false", "false", "false"); } ); });
well works. use jquery template system. in index.html script tag included. in other file include .on("pagebefore")
event, include 3 a
tag's. problem .hover
function doesn't works. when paste .hover
code in console works.
here jsfiddle
thanks in advice!
it might because elements created dynamically
$(document).on('mouseenter','#one',function () { markhover("true", "false", "false", "false", "false"); }).on('mouseleave', '#one', function () { markhover("false", "false", "false", "false", "false"); }); $(document).on('mouseenter','#two',function () { markhover("true", "true", "false", "false", "false"); }).on('mouseleave', '#two', function () { markhover("false", "false", "false", "false", "false"); });
you can simplify to
var params = { 'one': ["true", "false", "false", "false", "false"], 'two': ["true", "true", "false", "false", "false"], 'three': ["true", "true", "true", "false", "false"], 'four': ["true", "true", "true", "true", "false"], 'five': ["true", "true", "true", "true", "true"], } $(document).on('mouseenter','#one, #two, #three, #four, #five',function () { markhover.apply(window, params[this.id]); }).on('mouseleave', '#one, #two, #three, #four, #five', function () { markhover("false", "false", "false", "false", "false"); });
demo: fiddle
Comments
Post a Comment