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

jquery - How to detect a specific page load with javascript? - Stack Overflow

programmeradmin1浏览0评论

I have a gallery page with a table that has an id. Each table cell has an img element. How do I test if the current page has a table and those img elements?

I tried:

if ($("#gallery-table img"))
{
alert("Gallery page");
}

but that code runs on every page even if the elements do not exist.

I have a gallery page with a table that has an id. Each table cell has an img element. How do I test if the current page has a table and those img elements?

I tried:

if ($("#gallery-table img"))
{
alert("Gallery page");
}

but that code runs on every page even if the elements do not exist.

Share Improve this question asked Jun 26, 2011 at 18:22 user810606user810606
Add a ment  | 

1 Answer 1

Reset to default 9

Try

if ($("#gallery-table img").length)
{
   alert("Gallery page");
}

jQuery is returning an object no matter what, so you'll need to see if anything exists in that object.

发布评论

评论列表(0)

  1. 暂无评论