关于 JQuery 条件语句的帮助

发布于 2024-09-11 08:15:48 字数 551 浏览 2 评论 0原文

我试图在特定页面上运行一些 JQuery 脚本,但前提是加载特定页面。

场景:我们有一个加载页面(即,webpage.aspx),它根据引用点击加载不同的内容。 (即,webpage.aspx?contentId=1、webpage.aspx?contentId=2、webpage.aspx?contentId=3 等)。这是我的问题。如果只提取一个特定的 contentId,我需要删除页面的特定部分。我正在尝试在 JQuery 中执行此操作,但似乎无法弄清楚。

这是我迄今为止一直在做的事情。我意识到这是不正确的,但希望它能为您提供一个工作的起点。

谢谢。

代码:

$(window).load(function() {
   var $deleteNewRow = $("div.col.w140 tbody:first").find("td:first").parent().remove();
   if($('#dealer-info h1').indexOf('Ferrari') != -1) {
      $deleteNewRow
   }
});

I am trying to run some JQuery script on a particular page but only if a certain page loads.

The Scenario: We have one page that loads (i.e., webpage.aspx) and it loads different content based on the referring click. (i.e., webpage.aspx?contentId=1, webpage.aspx?contentId=2, webpage.aspx?contentId=3, etc). Here is my problem. I need to have a particular part of the page removed if only one specific contentId is pulled. I am trying to do this in JQuery and can't seem to figure it out.

Here's what i have been working with so far. I realize it's not correct but hopefully it gives you a starting point to work with.

Thanks.

CODE:

$(window).load(function() {
   var $deleteNewRow = $("div.col.w140 tbody:first").find("td:first").parent().remove();
   if($('#dealer-info h1').indexOf('Ferrari') != -1) {
      $deleteNewRow
   }
});

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

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

发布评论

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

评论(2

草莓味的萝莉 2024-09-18 08:16:18

您可以使用 jQuery :contains 选择器来检查是否存在。这是文档
然后要删除找到该元素,然后使用remove

$("div.col.w140 tbody:first").find("td:first").parent().remove();

You can use jQuery :contains selector to check for the existence. Here is the docs.
Then to delete find the element and then use remove.

$("div.col.w140 tbody:first").find("td:first").parent().remove();
病毒体 2024-09-18 08:16:11

您存储在 $deleteNewRow 变量中的不是 jQuery 方法,该方法已经执行。您想在 if 语句中执行该方法,如下所示(请注意,您还缺少 if 语句中的 .text() ):

$(window).load(function() {
   if($('#dealer-info h1').text().indexOf('Ferrari') != -1) {
      $("div.col.w140 tbody:first").find("td:first").parent().remove();
   }
});

What you store in your $deleteNewRow variable isn't the jQuery method, that method will already execute. You want to do that method in your if statement, something like this (note that you are also missing the .text() in the if statement):

$(window).load(function() {
   if($('#dealer-info h1').text().indexOf('Ferrari') != -1) {
      $("div.col.w140 tbody:first").find("td:first").parent().remove();
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文