我是 jquery 的新手,我确实尝试在网上找到一些东西,这就是我想出的。需要帮助来实现以下场景:
我如何编写一个 jQuery 来精确匹配下面的每一个?
div class="plugin-container plugin-news_home_feature"
div class="plugin-container plugin-news_home_feature plugin-section-wrapper"
无论我为第一个 div 类编写代码时,第二个上面的代码也会被捕获
这些我都试过了,但都没用
jQuery(".plugin-container.plugin-news_home_feature")
jQuery(div[class="plugin-container plugin-news_home_feature"])
jQuery(".plugin-container.plugin-news_home_feature").not(".plugin-container plugin-news_home_feature.plugin-section-wrapper")
我尝试了每个插件和没有插件之间的时间段,但没有任何效果。
提前致谢
---更新---
所以我想做的是使用 Google 跟踪代码管理器捕获一个事件,但可以说这些事件是在同一个存储桶中捕获的。这是我的。
$("div.plugin-container.plugin-news_home_feature").not('div.plugin-section-wrapper').on('click', function (e) {
dataLayer.push({
event: 'homeTracking',
action: 'Featured Stories under First Hero'
});
});
jQuery(".plugin-container.plugin-news_home_feature.plugin-section-wrapper").on('click', function (e) {
dataLayer.push({
event: 'homeTracking',
action: 'First Read Feature'
});
});
最佳答案
您将使用 :not()
CSS pseudo-class selector 在您的 JQuery 选择器中排除您不想要的那个。
$("div.plugin-container.plugin-news_home_feature:not(div.plugin-section-wrapper)").addClass("test");
.test { background-color:yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="plugin-container plugin-news_home_feature">First Div</div>
<div class="plugin-container plugin-news_home_feature plugin-section-wrapper">Second Div</div>
关于javascript - jQuery:如何精确匹配 Div 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50140996/