简体   繁体   English

当我使用模板时,Javascript函数不起作用

[英]Javascript function dosn't works when I work with templates

I have a html file with in a javascript code. 我有一个javascript代码的html文件。

<div id="star_rating">
    <a id="one">&#9734;</a><a id="two">&#9734;</a><a id="three">&#9734;</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 this works. 这很有效。 Now I use a jQuery template system. 现在我使用jQuery模板系统。 In the index.html is the script tag included. 在index.html中包含脚本标记。 In a other file that include with a .on("pagebefore") event, I include the three a tag's. 在包括有一个其它文件.on("pagebefore")事件,我有三个a标签的。 The problem is that the .hover function doesn't works. 问题是.hover功能不起作用。 When I paste the .hover code in my console it works. 当我将.hover代码粘贴到我的控制台时,它可以工作。

Here the jsFiddle 这里是jsFiddle

Thanks in advice! 谢谢你的建议!

It might be because the elements are 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 it 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 演示: 小提琴

Way too much code, not sure why it isn't working with your template but try this: 代码太多,不确定为什么它不能与你的模板一起使用但是试试这个:

jQuery: jQuery的:

   $(".rate").hover(
        function () {
            var thisone = parseInt($(this).data('star'));
            $(".rate").each(function(){ 
                if(parseInt($(this).data('star')) <= thisone)
                {
                    $(this).html("&#9733;");
                }
            });
        },
        function () {
            $(".rate").each(function(){
                $(this).html("&#9734;");
            });
        }               
    );

HTML: HTML:

<div>
    <a class="rate" id="one" data-star="1">&#9734;</a>
    <a class="rate" id="two" data-star="2">&#9734;</a>
    <a class="rate" id="three" data-star="3">&#9734;</a>
    <a class="rate" id="four" data-star="4">&#9734;</a>
    <a class="rate" id="five" data-star="5">&#9734;</a>
</div>

Fiddle Demo: http://jsfiddle.net/calder12/uXPt9/3/ 小提琴演示: http//jsfiddle.net/calder12/uXPt9/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM