最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery doesn't find by ID - Stack Overflow

programmeradmin1浏览0评论

I think Google Chrome console log is going to explain my problem best:

>> $(".single.portfolio")
[<article id=​"#js-single-item" class=​"post-883 portfolio type-portfolio status-publish hentry single">​…​</article>​]

>> the_element_id = $(".single.portfolio").attr("id")
"#js-single-item"

>> $(the_element_id)
[]

>> $("#js-single-item");
[]

>> document.getElementById("#js-single-item");
<article id=​"#js-single-item" class=​"post-883 portfolio type-portfolio status-publish hentry single">​…​</article>​

The weird thing is that getElementById works, but jQuery doesn't.

I tried to reproduce the problem in a fiddle by copying the whole HTML over and the code works as it's supposed to, so no trouble there. Most likely something is clashing together.

I'm looking for debugging tips. Thanks!

Edit: Typo. Problem Solved.

I think Google Chrome console log is going to explain my problem best:

>> $(".single.portfolio")
[<article id=​"#js-single-item" class=​"post-883 portfolio type-portfolio status-publish hentry single">​…​</article>​]

>> the_element_id = $(".single.portfolio").attr("id")
"#js-single-item"

>> $(the_element_id)
[]

>> $("#js-single-item");
[]

>> document.getElementById("#js-single-item");
<article id=​"#js-single-item" class=​"post-883 portfolio type-portfolio status-publish hentry single">​…​</article>​

The weird thing is that getElementById works, but jQuery doesn't.

I tried to reproduce the problem in a fiddle by copying the whole HTML over and the code works as it's supposed to, so no trouble there. Most likely something is clashing together.

I'm looking for debugging tips. Thanks!

Edit: Typo. Problem Solved.

Share Improve this question asked Jun 12, 2013 at 5:11 pyronaurpyronaur 3,5456 gold badges36 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11
id=​"#js-single-item"

The id attribute shouldn't include the # sign. When you select $('#some-id') in jQuery, it's actually calling document.getElementById('some-id').

You can do this:

$('#\\#js-single-item')

Also, don't.

You have # at the beginning of the id. since # is the selector for 'id' in jQuery, it will consider, it as element with id = js-single-item not #js-single-item

发布评论

评论列表(0)

  1. 暂无评论