![](/img/trans.png)
[英]How to add an “exception” or “condition” in this code (javascript, jquery)?
[英]How to add exception in this jquery code?
如何在此jQuery代碼中添加異常?
$(function() {
$("table tr:nth-child(even)").addClass("striped");
});
此代碼適用於所有表。
但是對於特定的頁面,我不希望出現剝離效果。
我在每個頁面上都有不同的正文ID。
我想知道如何為ID添加例外。
$(function() {
$("table tr:nth-child(even)").addClass("striped");
//I want to add exception to not to add striped class to only to page with <body id="nostrip">
});
如果您只需要過濾一個ID,David的解決方案就可以工作。 但是,由於您不想使用該腳本有多個正文ID,因此可以使用以下代碼:
$('body:not(#id1, #id2, #id3) tr:even').addClass('striped');
$('body[id!=nostrip] table tr:nth-child(even)').addClass("striped");
可以減少到
$('body[id!=nostrip] tr:even').addClass("striped");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.