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
425 views
in Technique[技术] by (71.8m points)

javascript - 检查jQuery中是否存在元素[重复](Check if element exists in jQuery [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

How do I check if an element exists if the element is created by .append() method?

(如果该元素是由.append()方法创建的,该如何检查该元素是否存在?)

$('elemId').length doesn't work for me.

($('elemId').length对我不起作用。)

  ask by Nick translate from so

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

1 Answer

0 votes
by (71.8m points)

$('elemId').length doesn't work for me.

($('elemId').length对我不起作用。)

You need to put # before element id:

(您需要将#放在元素ID之前:)

$('#elemId').length
---^

With vanilla JavaScript, you don't need the hash ( # ) eg document.getElementById('id_here') , however when using jQuery, you do need to put hash to target elements based on id just like CSS.

(使用香草JavaScript时,您不需要哈希( # ),例如document.getElementById('id_here') ,但是在使用jQuery时,您确实需要像CSS一样基于id将哈希放入目标元素。)


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

...