jQuery: Hide element on click anywhere else apart of the element

Possible Duplicate: How to detect a click outside an element? I'm trying to achieve that a 'div' hides if user clicks anywhere except on the element. I've got following code doing toggle() if user clicks on a button. I want the button click to stay plus if the 'Details' element is visible, reacts to other parts of the screen. $('.nav-toggle').click(function(){

jQuery:在元素的任何其他地方单击时隐藏元素

可能重复: 如何检测元素外的点击? 我试图实现一个'div'隐藏,如果用户点击除了元素以外的任何地方。 如果用户点击一个按钮,我有下面的代码做toggle()。 如果'Details'元素可见,我想让按钮点击保持加,并对屏幕的其他部分作出反应。 $('.nav-toggle').click(function(){ //get collapse content selector var collapse_content_selector = $(this).attr('href'); //mak

How to check htmlElement object exists in DOM

This question already has an answer here: Is there an “exists” function for jQuery? 36 answers 您可以检查DOM是否包含htmlElement if ($.contains(document, $(htmlElement)){ // htmlElement is attached to the DOM } In order to give proper condition you always need to check length of object ie if($("li.k-button.k-state-hover").length) 试用javascript: var ele = document.getEleme

如何在DOM中检查htmlElement对象

这个问题在这里已经有了答案: jQuery有没有“存在”功能? 36个答案 您可以检查DOM是否包含htmlElement if ($.contains(document, $(htmlElement)){ // htmlElement is attached to the DOM } 为了给出适当的条件,你总是需要检查物体的长度 即if($(“li.k-button.k-state-hover”)。length) 试用javascript: var ele = document.getElementById('yourId'); or var ele = document.getElementByTagName('tag n

Simple Javascript Execution

This question already has an answer here: Is there an “exists” function for jQuery? 36 answers 要检查页面中是否存在元素,可以选择它,然后检查length属性(如果使用jQuery)或者它为null(如果使用普通的JS): // jQuery if ($('.site-header__cart-indicator').length) window.setTimeout(redirectCheckout, 2000); // plain JS if (window.querySelector('.site-header__cart-indicator')) window.setTimeout

简单的Javascript执行

这个问题在这里已经有了答案: jQuery有没有“存在”功能? 36个答案 要检查页面中是否存在元素,可以选择它,然后检查length属性(如果使用jQuery)或者它为null(如果使用普通的JS): // jQuery if ($('.site-header__cart-indicator').length) window.setTimeout(redirectCheckout, 2000); // plain JS if (window.querySelector('.site-header__cart-indicator')) window.setTimeout(redirectCheckout, 2000); 那

check if element is inserted on page

Possible Duplicate: Is there an “exists” function for jQuery Is there a better way to check if the element is already inserted on page? I'm currently doing this and is working fine, but doesn't seem for me to be the best/fast way $(element).closest('body').length > 0 Thanks. Edit: I doesn't need to know if my element exists, I need to know if it's on page. 你可以使

检查页面上是否插入元素

可能重复: 有没有jQuery的“存在”功能 有没有更好的方法来检查元素是否已经插入页面? 我目前正在这样做,工作正常,但似乎并不是我最好的/快速的方式 $(element).closest('body').length > 0 谢谢。 编辑: 我不需要知道我的元素是否存在,我需要知道它是否在页面上。 你可以使用jQuery内置的.has功能,所以你的代码应该看起来像这样: $('body').has(element) 尝试这个 : $(element, 'body').size() > 0

Why does this if always return true?

This question already has an answer here: Is there an “exists” function for jQuery? 36 answers Why does $('#id') return true if id doesn't exist? 6 answers Because $(".deatils.dailyPrice:contains($)") is an object and not null objects are always evaluated as true in if tests. From the MDN : Any value that is not undefined, null, 0, NaN, or the empty string ("

为什么如果总是返回true?

这个问题在这里已经有了答案: jQuery有没有“存在”功能? 36个答案 为什么如果id不存在,$('#id')会返回true? 6个答案 因为$(".deatils.dailyPrice:contains($)")是一个对象,而不是空对象始终评估为true中if测试。 来自MDN: 任何不是未定义的值,null,0,NaN或空字符串(“”),以及包含值为false的布尔对象的任何对象在传递到条件语句时评估为true 你可能想要 if ($(".deatils.dailyPric

How can I check if append element already exists?

This question already has an answer here: Is there an “exists” function for jQuery? 36 answers Just do the next: Html code <input id="addImage" type="button" value="Add image"/> <div id="IndicatorImgDiv"> </div> Javascript code $("#addImage").click(function(){ if($("#IndicatorImgDiv img").length == 0){ $('#IndicatorImgDiv').append(

我如何检查append元素是否已经存在?

这个问题在这里已经有了答案: jQuery有没有“存在”功能? 36个答案 只要做下一件事: Html代码 <input id="addImage" type="button" value="Add image"/> <div id="IndicatorImgDiv"> </div> Javascript代码 $("#addImage").click(function(){ if($("#IndicatorImgDiv img").length == 0){ $('#IndicatorImgDiv').append($('<img />').attr("src", "htt

Check if element exists

Possible Duplicates: Is there an “exists” function for jQuery jQuery determining if element exists on page if(tr) is returning true when tr is not an element, how do I check whether it's an element that exists? var tr = $('#parts-table .no-data').parent(); $('.delete', row).bind('click', function (e) { that.delete(e.currentTarget); }); console.log(tr); if (tr) //returns true when it

检查元素是否存在

可能重复: 有没有jQuery的“存在”功能 jQuery确定页面上是否存在元素 if(tr)在tr不是元素时返回true,我该如何检查它是否存在的元素? var tr = $('#parts-table .no-data').parent(); $('.delete', row).bind('click', function (e) { that.delete(e.currentTarget); }); console.log(tr); if (tr) //returns true when it shouldn't 检查其length属性: if(tr.length) { // exists } if(tr)总是评估为真,因为

How do you check if a selector matches something in jQuery?

This question already has an answer here: Is there an “exists” function for jQuery? 36 answers As the other commenters are suggesting the most efficient way to do it seems to be: if ($(selector).length ) { // Do something } If you absolutely must have an exists() function - which will be slower- you can do: jQuery.fn.exists = function(){return this.length>0;} Then in your code you

你如何检查选择器是否匹配jQuery中的某些东西?

这个问题在这里已经有了答案: jQuery有没有“存在”功能? 36个答案 正如其他评论者所建议的那样,最有效的方法似乎是: if ($(selector).length ) { // Do something } 如果你绝对必须有一个exists()函数 - 这会更慢 - 你可以这样做: jQuery.fn.exists = function(){return this.length>0;} 然后在你的代码中你可以使用 if ($(selector).exists()) { // Do something } 在这里回答 不,jQuery总是返

Keep left column visible AND scrollable

It's easy to keep a column in my layout fixed so it's always visible, even when the user scrolls down. It's also easy to only move the column down the page when the page is scrolled down far enough for it to be out of the viewport so it's anchored before scrolling starts. My problem is, I have left hand column that is taller than the average window so you need to be able to sc

保持左列可见并可滚动

在我的布局中保留一个列很容易,因此即使用户向下滚动,它也始终可见。 当页面向下滚动时,只需将页面向下移动到页面以外,也很容易,因为它在滚动开始之前锚定。 我的问题是,我的左栏比平均窗口高 ,因此您需要向下滚动才能看到左栏中的所有内容(控件),但是在向上滚动的同时您要查看再次控制顶部。 以下是我想要完成的视觉效果: 所以左列总是占据窗口高度的100%,但是当用户向下滚动时,他们可以看到div的底部,

Delaying click event

I'm wondering whether there's a simple way to delay the click event from being processed for a specified period of time. For example we could have $('#someElement').on('click', 'a', function(event) { var duration = 1000; someAsynchronousFunction(); // Start as soon as click occurs ... // Code to delay page transition from taking place for duration specified }); So in this c

延迟点击事件

我想知道是否有一种简单的方法来延迟点击事件在指定的时间段内被处理。 例如,我们可以拥有 $('#someElement').on('click', 'a', function(event) { var duration = 1000; someAsynchronousFunction(); // Start as soon as click occurs ... // Code to delay page transition from taking place for duration specified }); 所以在这种情况下,异步函数将保证运行一段时间。 如果它没有完成它在这个时间的工作