2 回答
TA贡献1813条经验 获得超2个赞
这是执行此操作的一种更简单的原始方法。
const getCurrentProjectId = () => {
let postId = Array.from(document.querySelector('body').classList).find(cls => cls.indexOf('postid-') > -1).split('-')[1];
return `post-${postId}`;
}
console.log(getCurrentProjectId());
document.addEventListener('DOMContentLoaded', () => {
let currentProject = getCurrentProjectId();
document.querySelectorAll('.project').forEach(proj => {
if(proj.classList.contains(currentProject)) proj.classList.add('current-project');
});
});
.current-project {
border: 2px solid red;
}
<body class="project-template-default single single-project postid-688 logged-in admin-bar has-stick chrome windows et_button_icon_visible et_button_custom_icon et_pb_button_helper_class et_transparent_nav et_fullwidth_nav et_fullwidth_secondary_nav et_non_fixed_nav et_show_nav et_cover_background et_secondary_nav_enabled et_secondary_nav_two_panels et_pb_gutter et_pb_gutters3 et_primary_nav_dropdown_animation_fade et_secondary_nav_dropdown_animation_fade et_pb_footer_columns4 et_header_style_left et_pb_pagebuilder_layout et_smooth_scroll et_right_sidebar et_divi_theme et-db et_minified_js et_minified_css customize-support"<div class="et_pb_module et_pb_portfolio et_pb_portfolio_0 et_pb_bg_layout_light">
<div class="et_pb_ajax_pagination_container">
<div id="post-708" class="post-708 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/finite-element-analysis/" title="Finite Element Analysis">Finite Element Analysis</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-702" class="post-702 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/refinery-project-water-wash-system/" title="Refinery Project – Water Wash System">Refinery Project – Water Wash System</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-695" class="post-695 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/marcellus-compression-station/" title="Marcellus Compression Station">Marcellus Compression Station</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-693" class="post-693 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/houston-ship-channel-tank-farm-expansion/" title="Houston Ship Channel – Tank Farm Expansion">Houston Ship Channel – Tank Farm Expansion</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-688" class="post-688 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/process-safety-management-pha-lopa-analysis/" title="Process Safety Management – PHA & LOPA Analysis">Process Safety Management – PHA & LOPA Analysis</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
</div>
</div>
</body>
这是使用find的选项(假设只有一个帖子):
const getCurrentProjectId = () => `post-${Array.from(document.querySelector('body')
.classList)
.find(cls => cls.indexOf('postid-') > -1)
.split('-')[1]}`;
console.log(getCurrentProjectId());
document.addEventListener('DOMContentLoaded', () => {
let currentProject = getCurrentProjectId();
Array.from(document.querySelectorAll('.project'))
.find(proj => proj.classList.contains(currentProject)) .classList.add('current-project');
});
.current-project {
border: 2px solid red;
}
<body class="project-template-default single single-project postid-688 logged-in admin-bar has-stick chrome windows et_button_icon_visible et_button_custom_icon et_pb_button_helper_class et_transparent_nav et_fullwidth_nav et_fullwidth_secondary_nav et_non_fixed_nav et_show_nav et_cover_background et_secondary_nav_enabled et_secondary_nav_two_panels et_pb_gutter et_pb_gutters3 et_primary_nav_dropdown_animation_fade et_secondary_nav_dropdown_animation_fade et_pb_footer_columns4 et_header_style_left et_pb_pagebuilder_layout et_smooth_scroll et_right_sidebar et_divi_theme et-db et_minified_js et_minified_css customize-support"<div class="et_pb_module et_pb_portfolio et_pb_portfolio_0 et_pb_bg_layout_light">
<div class="et_pb_ajax_pagination_container">
<div id="post-708" class="post-708 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/finite-element-analysis/" title="Finite Element Analysis">Finite Element Analysis</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-702" class="post-702 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/refinery-project-water-wash-system/" title="Refinery Project – Water Wash System">Refinery Project – Water Wash System</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-695" class="post-695 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/marcellus-compression-station/" title="Marcellus Compression Station">Marcellus Compression Station</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-693" class="post-693 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/houston-ship-channel-tank-farm-expansion/" title="Houston Ship Channel – Tank Farm Expansion">Houston Ship Channel – Tank Farm Expansion</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-688" class="post-688 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/process-safety-management-pha-lopa-analysis/" title="Process Safety Management – PHA & LOPA Analysis">Process Safety Management – PHA & LOPA Analysis</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
</div>
</div>
</body>
这是一个考虑到BenM对ID的评论的选项
const getCurrentProjectId = () => `post-${Array.from(document.querySelector('body')
.classList)
.find(cls => cls.indexOf('postid-') > -1)
.split('-')[1]}`;
console.log(getCurrentProjectId());
document.addEventListener('DOMContentLoaded', () => {
document.querySelector(`#${getCurrentProjectId()}`).classList.add('current-project');
});
.current-project {
border: 2px solid red;
}
<body class="project-template-default single single-project postid-688 logged-in admin-bar has-stick chrome windows et_button_icon_visible et_button_custom_icon et_pb_button_helper_class et_transparent_nav et_fullwidth_nav et_fullwidth_secondary_nav et_non_fixed_nav et_show_nav et_cover_background et_secondary_nav_enabled et_secondary_nav_two_panels et_pb_gutter et_pb_gutters3 et_primary_nav_dropdown_animation_fade et_secondary_nav_dropdown_animation_fade et_pb_footer_columns4 et_header_style_left et_pb_pagebuilder_layout et_smooth_scroll et_right_sidebar et_divi_theme et-db et_minified_js et_minified_css customize-support"<div class="et_pb_module et_pb_portfolio et_pb_portfolio_0 et_pb_bg_layout_light">
<div class="et_pb_ajax_pagination_container">
<div id="post-708" class="post-708 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/finite-element-analysis/" title="Finite Element Analysis">Finite Element Analysis</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-702" class="post-702 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/refinery-project-water-wash-system/" title="Refinery Project – Water Wash System">Refinery Project – Water Wash System</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-695" class="post-695 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/marcellus-compression-station/" title="Marcellus Compression Station">Marcellus Compression Station</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-693" class="post-693 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/houston-ship-channel-tank-farm-expansion/" title="Houston Ship Channel – Tank Farm Expansion">Houston Ship Channel – Tank Farm Expansion</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
<div id="post-688" class="post-688 project type-project status-publish hentry et_pb_post et_pb_portfolio_item">
<h3 class="et_pb_module_header">
<a href="/project/process-safety-management-pha-lopa-analysis/" title="Process Safety Management – PHA & LOPA Analysis">Process Safety Management – PHA & LOPA Analysis</a>
</h3>
</div><!-- .et_pb_portfolio_item -->
</div>
</div>
</body>
TA贡献1826条经验 获得超6个赞
为什么您需要遍历所有div元素才能做到这一点?
由于ID属性应该始终是唯一的,因此您可以从body类中提取ID ,然后选择相关元素:
if( $('body').hasClass('single-project') )
{
var postID = /(postid-)[0-9]+/gm.exec($('body').attr('class'))[0].replace('postid-', '');
$('#post-' + postID).addClass('current-project');
}
添加回答
举报