本文翻译自:How do you check if a selector matches something in jQuery? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • Is there an “exists” function for jQuery? jQuery是否存在“存在”功能? 40 answers 40个答案

In Mootools, I'd just run if ($('target')) { ... } . 在Mootools中,我只运行if ($('target')) { ... } Does if ($('#target')) { ... } in jQuery work the same way? if ($('#target')) { ... } jQuery中的if ($('#target')) { ... }以相同的方式工作吗?


#1楼

参考:https://stackoom.com/question/1FzW/你如何检查选择器是否匹配jQuery中的内容-重复


#2楼

I prefer the 我更喜欢

    if (jQuery("#anyElement").is("*")){...}

Which basically checks if this elements is a kind of "*" (any element). 这基本上检查这些元素是否是一种“*”(任何元素)。 Just a cleaner syntax and the "is" makes more sense inside an "if" 只是一个更清晰的语法和“是”在“如果”内部更有意义


#3楼

firstly create a function: 首先创建一个函数:

$.fn.is_exists = function(){ return document.getElementById(selector) }

then 然后

if($(selector).is_exists()){ ... }

#4楼

jQuery.fn.exists = function(selector, callback) {var $this = $(this);$this.each(function() {callback.call(this, ($(this).find(selector).length > 0));});
};

#5楼

if ($('#elem')[0]) {// do stuff
}

#6楼

no, jquery always returns a jquery object regardless if a selector was matched or not. 不,jquery总是返回一个jquery对象,无论选择器是否匹配。 You need to use .length 你需要使用.length

if ( $('#someDiv').length ){}

你如何检查选择器是否匹配jQuery中的内容? [重复]相关推荐

  1. 元素匹配jquery中filter、find、children、contents、contains区别

    发一下牢骚和主题无关: 虽然大多数前端都用jquery,可是对jquery这几个选择器在使用上的异同,大家应当时平不会太意在,或者也是解半知一吧! 面下简略总结了下: 1.filter: .filte ...

  2. jquery选择器连续选择_JQuery中的选择器

    jquery选择器连续选择 It's time to write some JQuery now. Do check out the introductory article on JQuery fi ...

  3. js进阶 10-4 jquery中基础选择器有哪些

    js进阶 10-4 jquery中基础选择器有哪些 一.总结 一句话总结: 1.群组选择器用的符号是什么? 群组选择器,中间是逗号 2.jquery中基础选择器有哪些? 5种,类,id,tag,群组, ...

  4. JQuery中Ajax的Post提交中文乱码的解决方案

    出自:http://m.blog.csdn.net/blog/blueheart20/26370023 引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何 ...

  5. 如何检查变量是否是JavaScript中的数组? [重复]

    本文翻译自:How do you check if a variable is an array in JavaScript? [duplicate] This question already ha ...

  6. jQuery中,选择器既匹配开头又匹配结尾

    jQuery中,选择器既匹配开头又匹配结尾的方法: 1 [attr^=val]attr$=val 2 [attr^=val][attr$=val] 转载于:https://www.cnblogs.co ...

  7. jQuery 学习笔记一(认识jQuery jQuery选择器 jQuery中的DOM操作)

    第一章 认识jQuery jQuery代码风格 $(document).ready(function(){ //... }); 简化 $(function(){ //... }); jQuery对象转 ...

  8. jQuery框架学习第二天:jQuery中万能的选择器

    jQuery框架学习第一天:开始认识jQuery jQuery框架学习第二天:jQuery中万能的选择器 jQuery框架学习第三天:如何管理jQuery包装集 jQuery框架学习第四天:使用jQu ...

  9. jquery 获取id最大的元素_超全整理!JQuery中的选择器集合

    JQuery的选择器总结 选择器是获取元素并进行操作的一种方式,在使用CSS选择器时,开发人员有时候需要考虑浏览器是否支持某些选择器.而在JQuery中,使用选择器则不需要考虑这些问题,与传统的JS获 ...

最新文章

  1. mac安装完python怎么打开-MAC中怎么安装python
  2. Servlet的体系结构 || Servlet相关配置
  3. linux创建 / 删除用户及用户管理
  4. Servlet 参数读取
  5. linux下python 2.4升级到 python 2.5
  6. animation-fill-mode属性的理解
  7. 不小心将.gitignore中标记忽略类型的文件提交到了远端仓库,怎么办?
  8. mysql 递归查询所有父级
  9. 启用 Windows 功能 NetFx3 时出错,错误代码: -2146498529
  10. Python 数据分析与挖掘概述
  11. 深度学习基础之三分钟轻松搞明白tensor到底是个啥!看不懂的话我倒立洗头~~
  12. Eth-Trunk链路聚合
  13. Intel(R) WiFi Link 5100 AGN 破解无线网络 BT4正式版U盘启动
  14. 前端程序员开发技术栈(前端干货)
  15. 0基础光缆/光纤熔接教程
  16. 客户端专项测试-启动时间测试
  17. 人生若只如初见 何事秋风悲画扇
  18. SpringBoot切面应用-输出接口调用日志
  19. 谷歌浏览器(Chrome)如何安装crx插件?(详细步骤)
  20. 银河麒麟怎么卸载软件_常见问题·银河麒麟操作系统 麒麟操作系统 中标麒麟 麒麟软件官方网站...

热门文章

  1. 【612页】Android 大厂面试题及解析大全(中高级)
  2. 在IntentService中使用Toast与在Service中使用Toast的异同,intentservicetoast
  3. Android之AsyncTask源码解析
  4. uniapp禁止遮罩层下的页面滚动
  5. layui中监听select下拉框改变事件
  6. UOJ#460. 新年的拯救计划 构造
  7. springboot 没有跳转到指定页面
  8. React router 4 获取路由参数,跨页面参数
  9. 一些关键字表明变量属性值
  10. Hystrix的一个坑,queue中的run方法没有被执行?