Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryjavascript if-else_Javascript_Jquery - Fatal编程技术网

jqueryjavascript if-else

jqueryjavascript if-else,javascript,jquery,Javascript,Jquery,您好,我的网站上有明星排名系统,代码中有3张图片(半张、全张、空张),它们在鼠标悬停时也在变化。我想要的是,当我的排名超过$ranking2.5){ $('#ratingIcon').attr('class','squareV'); }否则{ $('#ratingIcon').attr('class','squareX'); } } $(函数(){ 初始化(); $('#ratingIcon')。单击(函数(){ 变量得分=$('[name=score]'); 如果((score.val()!=

您好,我的网站上有明星排名系统,代码中有3张图片(半张、全张、空张),它们在鼠标悬停时也在变化。我想要的是,当我的排名超过$ranking<2.5时,它应该显示不同的图片(红色的),如果没有,它将继续显示我使用的绿色图片

<script>

    function initRating(score) {
        var bar = $('#newBar');
        bar.raty({

            "score": score,
            "half": true,
            "starHalf"   : '/wp-content/themes/matt/review-page/img/star-half.png',
            "starOff"    : '/wp-content/themes/matt/review-page/img/star-empty.png',
            "starOn"     : '/wp-content/themes/matt/review-page/img/star-full.png',
            "click"      : function() {
                $('#ratingHidden').val('1');
            },
            "hints": ['', '', '', '', ''],
            "space": false,
            "mouseover": function() {
                $rating = $('#newBar').data('score');
                switch_icon($rating);
            },
            "mouseout": function() {


                if($('#ratingHidden').val() != '1') {
                    switch_icon(5);
                } else {
                    var val = $('[name=score]').val();
                    if(val=='') val = '0';
                    switch_icon(parseFloat(val));
                }
            }
        });
    }


     function switch_icon($rating) {
        if($rating > 2.5) {
            $('#ratingIcon').attr('class', 'squareV');
        } else {
            $('#ratingIcon').attr('class', 'squareX');
        }
    }

    $(function() {
        initRating();

        $('#ratingIcon').click(function() {
            var score = $('[name=score]');
            if((score.val() != '0' && score.val() != '') || $('#ratingHidden').val() == '0') {
                $('#newBar > img').each(function() {
                    $(this).attr('src', '/wp-content/themes/matt/review-page/img/star-empty.png');
                });
                score.val('0');
                switch_icon(0);
                $('#ratingHidden').val('1')
            } else {
                $('#ratingHidden').val('0');
                $('#ratingIcon').attr('class', 'squareV');
                switch_icon(5);
            }
        }).mouseenter(function() {
            if($('#ratingHidden').val() == '1') {
                switch_icon(0);
            }
            $('#newBar > img').each(function() {
                $(this).attr('src', '/wp-content/themes/matt/review-page/img/star-empty.png');
            });
        }).mouseleave(function() {
            var score = $('[name=score]').val();
            if($('#ratingHidden').val() == '1') {
                switch_icon(score);
            } else {
                switch_icon(5);
            }

            initRating(score);
        });
    });
</script>

功能初始评级(分数){
var bar=$(“#newBar”);
酒吧({
“分数”:分数,
“一半”:对,
“starHalf”:“/wp content/themes/matt/review page/img/star half.png”,
“starOff”:“/wp content/themes/matt/review page/img/star empty.png”,
“starOn”:“/wp content/themes/matt/review page/img/star full.png”,
“单击”:函数(){
$('#ratingHidden').val('1');
},
“提示”:['','','','',
“空间”:假,
“mouseover”:函数(){
$rating=$('#newBar')。数据('score');
开关图标(额定值);
},
“mouseout”:函数(){
if($('#ratingHidden').val()!=“1”){
开关_图标(5);
}否则{
var val=$('[name=score]')。val();
如果(val='')val='0';
开关_图标(parseFloat(val));
}
}
});
}
功能开关图标($rating){
如果($评级>2.5){
$('#ratingIcon').attr('class','squareV');
}否则{
$('#ratingIcon').attr('class','squareX');
}
}
$(函数(){
初始化();
$('#ratingIcon')。单击(函数(){
变量得分=$('[name=score]');
如果((score.val()!=“0”和&score.val()!=“0”)|$(“#分级隐藏”).val()=“0”){
$('#newBar>img')。每个(函数(){
$(this.attr('src','/wp content/themes/matt/review page/img/star empty.png');
});
分数.val('0');
开关_图标(0);
$(“#分级隐藏”).val('1')
}否则{
$('#ratingHidden').val('0');
$('#ratingIcon').attr('class','squareV');
开关_图标(5);
}
}).mouseenter(函数(){
if($('#ratingHidden').val()=='1'){
开关_图标(0);
}
$('#newBar>img')。每个(函数(){
$(this.attr('src','/wp content/themes/matt/review page/img/star empty.png');
});
}).mouseleave(函数(){
var score=$('[name=score]')。val();
if($('#ratingHidden').val()=='1'){
切换_图标(分数);
}否则{
开关_图标(5);
}
初始评分(分数);
});
});
我尝试使用

 function ($rating) {
            if($rating < 2.5) {
                $("starHalf").attr('src','/wp-content/themes/matt/review-page/img/star-halfr.png');
            }
        }
功能($rating){
如果($评级<2.5){
$(“starHalf”).attr('src','/wp content/themes/matt/review page/img/star halfr.png');
}
}
但是它不起作用。。有人能帮我吗?

首先,$(“starHalf”)是对的?这里不需要任何类或id选择器吗?为什么在$rating变量中使用“$”


另外,请检查您的控制台,并告诉我们您遇到的任何错误。

JavaScript中有任何错误吗?“不起作用”有点含糊不清。请更具体一点。请尝试
console.log($rating)
什么是
“starHalf”
?当您编写
$(“starHalf”)
时,您真正告诉jQuery的是查找DOM元素:
“starHalf”将在第一个代码函数上变成半个星形图像。我没有任何错误,但它只是没有更改任何内容。这应该是一个注释。你不是在建议一个解决方案。我得到了“uncaughtsyntaxerror:unexpectedtoken”(“onconsole@user3558931我可以猜他是作为答案发布的,因为他没有足够的代表来回答comment@blurfus是的…在发表我的评论后,我也这么认为。对我的问题有什么帮助吗