Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

javascript - 如何在jQuery中检查null对象(How to check null objects in jQuery)

I'm using jQuery and I want to check the existence of an element in my page.

(我正在使用jQuery,我想检查页面中是否存在元素。)

I have written following code, but it's not working:

(我写了下面的代码,但它不起作用:)

if($("#btext" + i) != null) {
    //alert($("#btext" + i).text());
    $("#btext" + i).text("Branch " + i);
}

How do I check the existence of the element?

(如何检查元素的存在?)

  ask by Prashant translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check the jQuery FAQ ...

(查看jQuery FAQ ...)

You can use the length property of the jQuery collection returned by your selector:

(您可以使用选择器返回的jQuery集合的length属性:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...