Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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 如何检测鼠标是否被单击而不是$(此)元素_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何检测鼠标是否被单击而不是$(此)元素

Javascript 如何检测鼠标是否被单击而不是$(此)元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,如果在元素内单击鼠标(此处.block),我想应用一些样式。如果单击元素,则通过$(this)获取该元素并设置其样式。之后,当用户单击$(this)元素之外的其他元素时,我想将其更改回默认样式。如何检测是否单击了除$(此)元素之外的鼠标 到目前为止的js脚本: $( document ).ready(function() { // when a block is clicked get that specific block $('.block').click(function

如果在元素内单击鼠标(此处
.block
),我想应用一些样式。如果单击元素,则通过
$(this)
获取该元素并设置其样式。之后,当用户单击
$(this)
元素之外的其他元素时,我想将其更改回默认样式。如何检测是否单击了除
$(此)
元素之外的鼠标

到目前为止的js脚本:

$( document ).ready(function() {

    // when a block is clicked get that specific block
    $('.block').click(function() {
        var block = $(this);
        block.css('background','red');
    });

    //if clicked other than the block do stuff

});

您可以在
主体上绑定单击事件,并使用
e.target
功能检查是否单击了
#a

$('div')。单击(函数(){
$(this.css('背景','红色')
})
$('body')。单击(函数(e){
如果($(e.target).is($(“#a”)){
}否则{
$('a').css('background','tomato'))
}
})
div{
宽度:100px;
高度:100px;
背景:番茄;
}

您可以尝试以下方法:-

$(document).ready(function () {

    // when a block is clicked get that specific block
    $('.block').click(function () {
        var block = $(this);
        block.css('background', 'red');
    });

    //if clicked other than the block do stuff
    $('html:not(.block)').click(function () {
        //your logic here
    });
});

更好的方法是使用
tabindex
和d css
:focus
选择器

.container{
边框:1px实心#333;
宽度:200px;
高度:200px;
}
.街区{
背景色:#ccc;
宽度:50px;
浮动:左;
高度:50px;
利润率:20px;
}
.块:焦点{
背景色:红色;
大纲:无
}

在JS中这样做就像

var globalRef;
var inside=document.queryselectoral('.inside');
document.querySelector('.block')。addEventListener('click',函数(e){
if(globalRef!=未定义){
globalRef.style.background='none'
}
globalRef=e.target;
e、 target.style.background='red'
},假)
.block{
宽度:200px;
高度:200px;
背景:#ccc;
}
.里面{
显示:内联块;
宽度:100px;
高度:100px;
边框:1px纯绿色;
框大小:边框框;
浮动:左
}

添加一个CSS类,该类表示当前选定块的样式和选定状态:

.block.selected {
  background: red;
}
在click处理程序中,从选定的块中删除该类。(如果未选择任何块,
removeClass()
将不会执行任何操作。)然后将该类添加到新单击的块:

$('.block').click(function() {
  $('.block.selected').removeClass('selected');
  $(this).addClass('selected');
});

更新


如何知道它是在块内单击还是在其他.block内单击

用于确定是否已单击同一块两次:

$('.block').click(function() {
  if($(this).hasClass('selected')) {
    // do something here
  }

  $('.block.selected').removeClass('selected');
  $(this).addClass('selected');
});

请参见下面工作示例代码中的注释

<!doctype html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <style>
            .block {
                background: gray;
                width: 100px;
                height: 100px;
                float: left;
                margin-left: 20px;
            }
        </style>
    </head>
    <body>
        <div class="block" id="block1"></div>
        <div class="block" id="block2"></div>
        <div class="block" id="block3"></div>
        <script type="text/javascript">
            $(document).ready(function() {
                /*
                Solution 1:
                This gathers all elements with the class 'block' and defines a click event on it.
                In the click event all elements with class 'block' are walked through. If an element
                has the same id as the current element's id, than do one thing else do another thing.
                In this example background color en content of the three divs are changed
                */
                $('.block').click(function(i) {
                    /*
                    Notice the usage of $(this) and this in the code below. $(this) Is the
                    jquery element while 'this' is the DOM element.
                    */
                    var block = $(this);
                    $(".block").each(function(){
                        if (this.id == block.attr("id")) {
                            this.innerHTML = ("My id is " + this.id);
                            this.style.background = "red";
                        } else {
                            this.innerHTML = "";
                            this.style.background = "gray";
                        }
                    })
                });
                /*
                Solution 2:
                For each element you want actions on, define a separate click event.
                */
                $('#block1').click(function(i) {
                  alert("You clicked me?");
                });
                $('#block2').click(function(i) {
                  alert("Why did you click me?");
                });
                $('#block3').click(function(i) {
                  alert("Hi there, what's new?");
                });
            });
        </script>
    </body>
</html>

.街区{
背景:灰色;
宽度:100px;
高度:100px;
浮动:左;
左边距:20px;
}
$(文档).ready(函数(){
/*
解决方案1:
这将使用类“block”收集所有元素,并在其上定义单击事件。
在click事件中,将遍历类为“block”的所有元素。如果元素
具有与当前元素的id相同的id,而不是执行一项操作或执行另一项操作。
在本例中,三个div的背景色en内容被更改
*/
$('.block')。单击(函数(i){
/*
注意下面代码中$(this)和this的用法。$(this)是
jquery元素,而“this”是DOM元素。
*/
var块=$(此);
$(“.block”)。每个(函数(){
if(this.id==block.attr(“id”)){
this.innerHTML=(“我的id是”+this.id);
this.style.background=“红色”;
}否则{
this.innerHTML=“”;
this.style.background=“灰色”;
}
})
});
/*
解决方案2:
对于要对其执行操作的每个元素,定义一个单独的单击事件。
*/
$(“#块1”)。单击(函数(i){
警惕(“你点击了我?”);
});
$(“#块2”)。单击(函数(i){
警惕(“你为什么点击我?”);
});
$(“#块3”)。单击(函数(i){
警惕(“你好,有什么新鲜事吗?”);
});
});

根据Akshay的回答:

$('body').click(function (e) {
    var clicked = $(e.target);

    //See if the clicked element is (or is inside) a .block:
    var clickedBlock = clicked.closest('.block');
    if (clickedBlock.length) {
        clickedBlock.css('background', 'red');
    }

    //Remove the overridden background from other blocks:
    var allBlocks = $('.block');
    allBlocks.not(clickedBlock).css('background', '');
});

JSFiddle:

您必须通过简单的css来实现它。看我的answer@AnkitAgarwal是的,我看到了你的答案,它真的很有趣。但我也需要它来执行其他jquery函数+1.由我决定。这真的很好。好吧,如果你还有其他回调,你可以通过收听该块上的
focus
事件来实现。如果单击任何子元素,你想停止吗?@RejithRKrishnan,很抱歉,我没有理解你。你说停是什么意思?嗨,谢谢。但我想你误解了我的问题。我想要的块是实际的块obj,而不是块类。请你相应地修改一下好吗?@Aamu我不明白你的意思。你能不能在上面贴一个功能性的例子来说明你的问题。你好,谢谢。但是如果没有单击block obj,我需要更改,因为有许多
.block
s。请你换一下好吗。请您根据我的JSFIDLE更新您的答案。@Aamu这里有一个在您的场景中实现的脚本。我尝试使用您的脚本,但它不起作用。你能试试我的.Updated fiddle吗?它使用jQuery的
.closest()
来查找任何单击元素的容器
.block
:我如何知道它是在块内还是在其他.block或任何其他元素内单击的?JS控件仍然可以:在
文档上绑定一个单击侦听器,p