博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
你如何检查选择器是否匹配jQuery中的内容? [重复]
阅读量:2289 次
发布时间:2019-05-09

本文共 1130 字,大约阅读时间需要 3 分钟。

本文翻译自:

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

  • 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楼

参考:


#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 ){}

转载地址:http://dvcnb.baihongyu.com/

你可能感兴趣的文章
poj3414——Pots(BFS)
查看>>
fzu2150——Fire Game(BFS)
查看>>
poj3984——迷宫问题(BFS)
查看>>
hdu1241——Oil Deposits(DFS)
查看>>
hdu1495——非常可乐(BFS)
查看>>
hdu2612——Find a way(BFS)
查看>>
51nod1080——两个数的平方和
查看>>
51nod1136——欧拉函数
查看>>
poj1284——Primitive Roots(欧拉函数)
查看>>
1~n中所有数的欧拉函数值
查看>>
lightoj1370——Bi-shoe and Phi-shoe(欧拉函数应用)
查看>>
Lightoj1341——Aladdin and the Flying Carpet(算术基本定理)
查看>>
Lightoj1336——Sigma Function(因子和)
查看>>
Lightoj1282——Leading and Trailing(幂取模求前三位)
查看>>
Lightoj1259——Goldbach`s Conjecture(哥德巴赫猜想)
查看>>
hdu1848——Fibonacci again and again(SG函数)
查看>>
hdu5734——Acperience(数学推导)
查看>>
hdu5742——It's All In The Mind(模拟)
查看>>
hdu5744——Keep On Movin(构造回文)
查看>>
hdu5776——sum(抽屉原理)
查看>>