作者:-1的人生 | 来源:互联网 | 2023-05-17 10:31
Ihavetosetaspecificvaluetoanelementiftheelementisexists.如果元素存在,我必须为元素设置特定值。vara
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 个解决方案