如何检查JQuery中是否存在元素?

时间:2022-08-17 07:19:43

I have to set a specific value to an element if the element is exists.

如果元素存在,我必须为元素设置特定值。

var a = jQuery("#abc");
if(a) {
   a.val("something");
}

For this, I've to check a.length to check the element is exits.

为此,我要检查a.length以检查元素是否退出。

What happen if I directly set the value without checking the element is present or not?

如果我直接设置值而不检查元素是否存在会发生什么?

Because, If I do the following

因为,如果我这样做

jQuery("#abc").val("dfd");

I don't get any error in chrome when the element is not present. So, can I continue to use like this?

当元素不存在时,我没有在chrome中出现任何错误。那么,我可以继续这样使用吗?

or

any workaround?

Help appreciated!

4 个解决方案

#1


2  

What happen if I directly set the value without checking the element is present or not?

如果我直接设置值而不检查元素是否存在会发生什么?

Nothing. Calling jQuery methods on an empty jQuery object (set) doesn't cause a problem, it just does nothing. This is one of the great things about the set-based concept used in jQuery. The equivalent DOM code (document.getElementById("abc").value = "something";) would throw an error, but the jQuery version doesn't.

没有。在空的jQuery对象(set)上调用jQuery方法不会导致问题,它什么都不做。这是jQuery中使用的基于集合的概念的优点之一。等效的DOM代码(document.getElementById(“abc”)。value =“something”;)会抛出错误,但jQuery版本却没有。

Specifically, if the jQuery set is empty:

具体来说,如果jQuery集为空:

  • Calling setter methods (like your val call) becomes a no-op.

    调用setter方法(比如你的val调用)就变成了无操作。

  • Calling getter methods — for instance, var x = $("#foo").val(); — returns the value undefined.

    调用getter方法 - 例如,var x = $(“#foo”)。val(); - 返回未定义的值。

  • Calling traversal methods — for instance, var divs = $("#foo).find("div"); — gives you a new empty set.

    调用遍历方法 - 例如,var divs = $(“#foo).find(”div“); - 为您提供一个新的空集。

You only need to check (using if (a.length) as you said, or if (a[0])) if you actually care.

你只需要检查(如你所说的那样使用if(a.length),或者如果你真正关心的话,是否(a [0]))。

jQuery("#abc").val("dfd");

I don't get any error in chrome when the element is not present. So, can I continue to use like this?

当元素不存在时,我没有在chrome中出现任何错误。那么,我可以继续这样使用吗?

Yup.

#2


0  

jQuery's val() method simply sets (or gets) the value of each matching element. If there are no matching element, there will be no value to set (or get). You don't need to check if the element exists first.

jQuery的val()方法只是设置(或获取)每个匹配元素的值。如果没有匹配元素,则没有设置(或获取)的值。您无需先检查元素是否存在。

From jQuery's val() documentation:

从jQuery的val()文档:

Description: Set the value of each element in the set of matched elements.

描述:设置匹配元素集中每个元素的值。

If there are no matched elements, nothing will happen.

如果没有匹配的元素,则不会发生任何事情。

#3


0  

Try with -

尝试 -

jQuery("#abc").length > 0

#4


0  

Yes, you can safely continue. JQuery just executes a function on all elements found by the selector - if there are none, it does nothing. There's no error.

是的,你可以安全地继续。 JQuery只对选择器找到的所有元素执行一个函数 - 如果没有,它什么都不做。没有错误。

#1


2  

What happen if I directly set the value without checking the element is present or not?

如果我直接设置值而不检查元素是否存在会发生什么?

Nothing. Calling jQuery methods on an empty jQuery object (set) doesn't cause a problem, it just does nothing. This is one of the great things about the set-based concept used in jQuery. The equivalent DOM code (document.getElementById("abc").value = "something";) would throw an error, but the jQuery version doesn't.

没有。在空的jQuery对象(set)上调用jQuery方法不会导致问题,它什么都不做。这是jQuery中使用的基于集合的概念的优点之一。等效的DOM代码(document.getElementById(“abc”)。value =“something”;)会抛出错误,但jQuery版本却没有。

Specifically, if the jQuery set is empty:

具体来说,如果jQuery集为空:

  • Calling setter methods (like your val call) becomes a no-op.

    调用setter方法(比如你的val调用)就变成了无操作。

  • Calling getter methods — for instance, var x = $("#foo").val(); — returns the value undefined.

    调用getter方法 - 例如,var x = $(“#foo”)。val(); - 返回未定义的值。

  • Calling traversal methods — for instance, var divs = $("#foo).find("div"); — gives you a new empty set.

    调用遍历方法 - 例如,var divs = $(“#foo).find(”div“); - 为您提供一个新的空集。

You only need to check (using if (a.length) as you said, or if (a[0])) if you actually care.

你只需要检查(如你所说的那样使用if(a.length),或者如果你真正关心的话,是否(a [0]))。

jQuery("#abc").val("dfd");

I don't get any error in chrome when the element is not present. So, can I continue to use like this?

当元素不存在时,我没有在chrome中出现任何错误。那么,我可以继续这样使用吗?

Yup.

#2


0  

jQuery's val() method simply sets (or gets) the value of each matching element. If there are no matching element, there will be no value to set (or get). You don't need to check if the element exists first.

jQuery的val()方法只是设置(或获取)每个匹配元素的值。如果没有匹配元素,则没有设置(或获取)的值。您无需先检查元素是否存在。

From jQuery's val() documentation:

从jQuery的val()文档:

Description: Set the value of each element in the set of matched elements.

描述:设置匹配元素集中每个元素的值。

If there are no matched elements, nothing will happen.

如果没有匹配的元素,则不会发生任何事情。

#3


0  

Try with -

尝试 -

jQuery("#abc").length > 0

#4


0  

Yes, you can safely continue. JQuery just executes a function on all elements found by the selector - if there are none, it does nothing. There's no error.

是的,你可以安全地继续。 JQuery只对选择器找到的所有元素执行一个函数 - 如果没有,它什么都不做。没有错误。