如何使用 javascript 检测特定页面加载?

发布于 2024-11-17 00:45:32 字数 202 浏览 0 评论 0原文

我有一个画廊页面,其中有一个带有 id 的表。每个表格单元格都有一个 img 元素。如何测试当前页面是否有表格和那些 img 元素?

我尝试过:

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

但是即使元素不存在,该代码也会在每个页面上运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独陪着我 2024-11-24 00:45:32

尝试jQuery 都会返回一个对象,因此您需要查看该对象中是否存在任何内容。

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

无论如何,

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文