javascript - 将 JavaScript 分配给某个 body 类
问题描述
我需要在特定页面上运行此脚本,但也只针对特定的正文类。示例:在此页面上有<body class="category-type-plp">
如何将我的脚本分配给“category-type-plp”?
plpSpaceRemove: function() {
$(document).ready(function() {
if ($('.bv-tile.bv-inline-rating-outer').length % 4 === 0) {
for (let i = 0; i < ($('.bv-tile.bv-inline-rating-outer').length / 4); i++) {
let remove = true;
$('.bv-tile.bv-inline-rating-outer').slice(i * 4, 4 * (i + 1)).each(function() {
if ($(this).find(".bv-stars-wrap
").length != 0) {
remove = false;
}
});
if (remove) {
$('.bv-tile.bv-inline-rating-outer').slice(i * 4, 4 * (i + 1)).each(function() {
if ($(this).find(".bv-stars-wrap
").length === 0) {
$(this).css("display", "none");
}
});
}
}
}
else {
for (let i = 0; i < ($('.bv-tile.bv-inline-rating-outer').length / 3); i++) {
let remove = true;
$('.bv-tile.bv-inline-rating-outer').slice(i * 3, 3 * (i + 1)).each(function() {
if ($(this).find('.bv-stars-wrap
').length != 0) {
remove = false;
}
});
解决方案
您可以检查您的身体是否有某个类别:
if (!$('body').hasClass('some-class')) return;
推荐阅读
- android - 需要通过传递访问令牌在 android webview 中访问托管在 Azure MSAL 中的 Web 应用程序
- python-3.x - 将 LinMot 电机的 .NET DLL 导入 python 并使用其功能
- json - 在 JQ 查询中使用变量
- hadoop - ssh 连接到主机端口 22 连接被拒绝
- swift - Xcode 12、工作区和新的“XCFrameworks”不能一起工作
- html - 是否可以将 ui-kit offcanves 添加为下拉菜单?
- reactjs - 如何通过反应获取天蓝色广告刷新令牌
- ios - iPhone12 mini中,状态栏高度是44,为什么tableView的frame.y = 50?
- python - 使用请求和 BeautifulSoup 登录以抓取页面
- android - 按下时如何更改本机TouchableOpacity颜色