Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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
Javascript jQuery检测鼠标是否在mouseenter和mouseleave之间单击_Javascript_Jquery_Mouseevent - Fatal编程技术网

Javascript jQuery检测鼠标是否在mouseenter和mouseleave之间单击

Javascript jQuery检测鼠标是否在mouseenter和mouseleave之间单击,javascript,jquery,mouseevent,Javascript,Jquery,Mouseevent,我正在为评级系统编写一个小脚本,目前有3个功能: 悬停: a. On mouse enter change the color of div depending on value b. On mouse leave revert back to white onclick: a. On click save color change and put value in another div 现在,我需要检查在鼠标移动之前是否单击了其中一个div,因为如果选择了它,我需要颜色与鼠标离开div

我正在为评级系统编写一个小脚本,目前有3个功能:

  • 悬停:

    a. On mouse enter change the color of div depending on value
    b. On mouse leave revert back to white
    
  • onclick:

    a. On click save color change and put value in another div
    
    现在,我需要检查在鼠标移动之前是否单击了其中一个div,因为如果选择了它,我需要颜色与鼠标离开div时单击的颜色保持一致

  • 我该怎么查呢

    以下是我的部门结构:

    <div class='rating' data-target="tijd">
        <div class='circle'>1</div>//green
        <div class='circle'>2</div>//green
        <div class='circle'>3</div>//green
        <div class='circle'>4</div>//green
        <div class='circle'>5</div>//green
        <div class='circle'>6</div>//green
        <div class='circle'>7</div>//green
        <div class='circle'>8</div>//white
        <div class='circle'>9</div>//white
        <div class='circle'>10</div>//white
        <div class='score'><span id="tijd">7</span></div>
    </div>
    
    .reviews{
    背景色:白色;
    显示:表格;
    宽度:100%;
    }
    .评级{
    显示:表格;
    表布局:固定;
    边界间距:10px;
    }
    .圆圈{
    显示:表格单元格;
    高度:25px;
    宽度:25px;
    文本对齐:居中;
    边框:纯色2px黑色;
    边界半径:100%;
    背景色:白色;
    }
    
    塔里夫
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9
    10
    贝雷克巴雷希德反应器
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9
    10
    
    我会使用CSS和一个类:

  • 使用CSS
    :hover
    规则设置鼠标光标位于元素上方时的颜色
  • 单击后,在元素上设置一个类,以指示它已被选中
  • 基于该类设置元素的样式
  • 示例:

    $(document.body).on(“单击“,”.rating>.circle”,函数(){
    $(this).toggleClass(“选定”).sides().removeClass(“选定”);
    });
    
    .rating{
    填充:2px;
    }
    .评级.圈{
    填充:6px;
    显示:内联块;
    宽度:2米;
    文本对齐:居中;
    }
    /*悬停时,请使用黄色背景*/
    .rating>.circle:悬停{
    背景颜色:黄色;
    }
    /*所选项目具有蓝色背景和白色文本*/
    .rating>.circle.selected,
    .rating>.circle.选定:悬停{
    背景颜色:蓝色;
    颜色:白色
    }
    
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9
    10
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9
    10
    
    我会使用CSS和一个类:

  • 使用CSS
    :hover
    规则设置鼠标光标位于元素上方时的颜色
  • 单击后,在元素上设置一个类,以指示它已被选中
  • 基于该类设置元素的样式
  • 示例:

    $(document.body).on(“单击“,”.rating>.circle”,函数(){
    $(this).toggleClass(“选定”).sides().removeClass(“选定”);
    });
    
    .rating{
    填充:2px;
    }
    .评级.圈{
    填充:6px;
    显示:内联块;
    宽度:2米;
    文本对齐:居中;
    }
    /*悬停时,请使用黄色背景*/
    .rating>.circle:悬停{
    背景颜色:黄色;
    }
    /*所选项目具有蓝色背景和白色文本*/
    .rating>.circle.selected,
    .rating>.circle.选定:悬停{
    背景颜色:蓝色;
    颜色:白色
    }
    
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9
    10
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9
    10
    
    假设元素的默认背景色定义为

    div.rating .circle
    {
       background-color: #000; 
    }
    
    悬停时颜色更改为

    div.rating .circle:hover
    {
       background-color: #ff0; 
    }
    
    在click事件中,设置另一个类“selected”,以便在鼠标离开时将颜色更改为
    #dedede

    $( "div.rating .circle" ).click( function(){
    
       $( this ).toggleClass( "selected" );
    
    } );
    
    div.rating .circle.selected
    {
       background-color: #dedede; 
    }
    
    还定义所选
    类的悬停行为

    div.rating .circle.selected:hover
    {
       background-color: #dedede; 
    }
    

    假设元素的默认背景色定义为

    div.rating .circle
    {
       background-color: #000; 
    }
    
    悬停时颜色更改为

    div.rating .circle:hover
    {
       background-color: #ff0; 
    }
    
    在click事件中,设置另一个类“selected”,以便在鼠标离开时将颜色更改为
    #dedede

    $( "div.rating .circle" ).click( function(){
    
       $( this ).toggleClass( "selected" );
    
    } );
    
    div.rating .circle.selected
    {
       background-color: #dedede; 
    }
    
    还定义所选
    类的悬停行为

    div.rating .circle.selected:hover
    {
       background-color: #dedede; 
    }
    

    我想在鼠标输入和鼠标离开时添加和删除一个类,并在单击时检查该类将是一种方便的方法

    //add and remove a focus class with the same css values as the active class
    $('.yourclass').on('mouseenter',function(){
        $('.yourclass').addClass('focussed');
    }).on('mouseleave',function(){
        $('.yourclass').removeClass('focussed');
    });
    
    //check only for clicks on the focussed element and add the active class
    $('.yourclass.focussed').on('click',function(){
        $(this).addClass('active');
    });
    

    我想在鼠标输入和鼠标离开时添加和删除一个类,并在单击时检查该类将是一种方便的方法

    //add and remove a focus class with the same css values as the active class
    $('.yourclass').on('mouseenter',function(){
        $('.yourclass').addClass('focussed');
    }).on('mouseleave',function(){
        $('.yourclass').removeClass('focussed');
    });
    
    //check only for clicks on the focussed element and add the active class
    $('.yourclass.focussed').on('click',function(){
        $(this).addClass('active');
    });
    

    您可以通过向jQuery中添加
    。click()
    函数来实现这一点,如下所示:

    请注意,所有这些都是示例代码 HTML jQuery**


    希望这有帮助

    您可以通过向jQuery中添加
    .click()
    函数来完成此操作,如下所示:

    请注意,所有这些都是示例代码 HTML jQuery**

    希望这有帮助

    可以使用jquerys.hover()函数代替mousein mouseout

    HTML

    <div class="boxy">
    
    </div>
    
    <div class="selected">
      <h1>Selected Color From Click</h1>
    </div>
    
    JS

    * {box-sizing: border-box;}
    .boxy{
      border: 2px solid #000;
      width: 400px;
      height: 400px;
    }
    .selected{
      border: 2px solid #000;
    }
    
    var colors = ["#34f23a", "#f8f8f1", "#31b1cc"];
    var changecolors;
    var colorIndex = 0;
    
    $('.boxy').hover(function(){
      shuffle = true;
      $(this).css('background', colors[colorIndex]);
      changecolors = setInterval(function(){
        if (colorIndex < (colors.length - 1)){
          colorIndex += 1;
        } else {
          colorIndex = 0;
        }
        $('.boxy').css('background', colors[colorIndex]);
      },500);
    
    }, function(){
      window.clearInterval(changecolors);
      $('.boxy').css('background', 'transparent');
    });
    
    $('.boxy').click(function(){
       var selectedColor = $(this).css('background');
       $('.selected').css('background', selectedColor);
    });
    
    var colors=[“#34f23a”、“#f8f8f1”、“#31b1cc”];
    颜色变化;
    var指数=0;
    $('.boxy').hover(函数(){
    洗牌=正确;
    $(this.css('background',colors[colorIndex]);
    changecolors=setInterval(函数(){
    如果(颜色索引<(colors.length-1)){
    色度指数+=1;
    }否则{
    颜色指数=0;
    }
    $('.boxy').css('background',colors[colorIndex]);
    },500);
    },函数(){
    窗口。清除间隔(更改颜色);
    $('.boxy').css('background','transparent');
    });
    $('.boxy')。单击(函数(){
    var selectedColor=$(this.css('background');
    $('.selected').css('background',selectedColor);
    });
    

    您可以使用jquerys.hover()函数代替mousein mouseout

    HTML

    <div class="boxy">
    
    </div>
    
    <div class="selected">
      <h1>Selected Color From Click</h1>
    </div>
    
    JS

    * {box-sizing: border-box;}
    .boxy{
      border: 2px solid #000;
      width: 400px;
      height: 400px;
    }
    .selected{
      border: 2px solid #000;
    }
    
    var colors = ["#34f23a", "#f8f8f1", "#31b1cc"];
    var changecolors;
    var colorIndex = 0;
    
    $('.boxy').hover(function(){
      shuffle = true;
      $(this).css('background', colors[colorIndex]);
      changecolors = setInterval(function(){
        if (colorIndex < (colors.length - 1)){
          colorIndex += 1;
        } else {
          colorIndex = 0;
        }
        $('.boxy').css('background', colors[colorIndex]);
      },500);
    
    }, function(){
      window.clearInterval(changecolors);
      $('.boxy').css('background', 'transparent');
    });
    
    $('.boxy').click(function(){
       var selectedColor = $(this).css('background');
       $('.selected').css('background', selectedColor);
    });
    
    var colors=[“#34f23a”、“#f8f8f1”、“#31b1cc”];
    颜色变化;
    var指数=0;
    $('.boxy').hover(函数(){
    洗牌=正确;
    $(this.css('background',colors[colorIndex]);
    changecolors=setInterval(函数(){
    如果(颜色索引<(colors.length-1)){
    色度指数+=1;
    }否则{
    颜色指数=0;
    }
    $('.boxy').css('background',colors[colorIndex]);
    },500);
    },函数(){
    窗口。清除间隔(更改颜色);
    $('.boxy').css('background','transparent');
    });
    $('.boxy')。单击(函数(){
    var selectedColor=$(this.css('background');
    $('.selected').css('background',selectedColor);
    });
    

    
    js函数
    正文{背景色:#101010;颜色:#aaa;文本对齐:居中;}
    #保持架{宽度:100%;高度:100px;边距:0自动;显示:内联块;}
    #悬停{位置:相对;边框:1px虚线#333;宽度:100px;他