简体   繁体   English

帮助JQuery条件语句

[英]Help with JQuery conditional statement

I am trying to run some JQuery script on a particular page but only if a certain page loads. 我试图在特定页面上运行一些JQuery脚本,但仅在某个页面加载时。

The Scenario: We have one page that loads (ie, webpage.aspx) and it loads different content based on the referring click. 场景:我们有一个加载的页面(即webpage.aspx),它根据引用的点击加载不同的内容。 (ie, webpage.aspx?contentId=1, webpage.aspx?contentId=2, webpage.aspx?contentId=3, etc). (即,webpage.aspx?contentId = 1,webpage.aspx?contentId = 2,webpage.aspx?contentId = 3等)。 Here is my problem. 这是我的问题。 I need to have a particular part of the page removed if only one specific contentId is pulled. 如果只提取一个特定的contentId,我需要删除页面的特定部分。 I am trying to do this in JQuery and can't seem to figure it out. 我试图在JQuery中这样做,似乎无法搞清楚。

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
   }
});

What you store in your $deleteNewRow variable isn't the jQuery method, that method will already execute. 存储在$deleteNewRow变量中的不是jQuery方法,该方法已经执行。 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): 你想在你的if语句中做这样的方法,比如这样(注意你也缺少if语句中的.text() ):

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

You can use jQuery :contains selector to check for the existence. 您可以使用jQuery :contains selector来检查是否存在。 Here is the docs . 这是文档 Then to delete find the element and then use remove . 然后删除查找元素然后使用remove

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM