在jQuery中为indexOf关键字匹配添加关键字排除
问题描述:
当前匹配的关键字然后执行操作。如何将排除列表纳入下面的功能?在jQuery中为indexOf关键字匹配添加关键字排除
<!-- Product names include Apple iPad, Acer Iconia, Lenovo Thinkpad plus Generic, No Brand-->
<h1 id="pbtitle">Apple iPad 3</h1>
<div class="install_option" style="display:none;">
<div style="box-sizing:border-box; float:left;">
<a href="https://www.example.com/acc" target="_blank">
<h3 style="font-weight:bold;">Would you like to view accessories?</h3>
</a>
</div>
</div>
var options = ['Acer','Apple','Lenovo'];
var exclusions = ['Generic','No Brand'];
var text = $("#pbtitle").text();
options.forEach(function(element) {
if (text.indexOf(element) != -1) { // if its NOT -1 (-1 = not found)
$('.install_option').css('display','block'); // then we found it .. do your magic
}
});
答
如果文本必须是夹杂物/排除的子字符串,然后
if (options.indexOf(text) != -1 && exclusions.indexOf(text) == -1)
你需要尝试
var options = ['Cooker hood','Island','Ceiling','Downdraft','Integrated','Canopy','Telescopic','Cabair','Bathroom'];
var exclusions = ['Filters','Filter','Kit','Foil','Ducting','Switch','Transformer','Silencers', 'Install'];
var text = $("#pbtitle").text();
if (options.indexOf(text) != -1 && exclusions.indexOf(text) == -1)
{
$('.install_option').css('display','block');
}
else
{
$('.install_option').css('display','none');
}
排除应该是'== -1' –
隐而不宣”还没有工作。排除是否需要添加到forEach? – me9867
options.forEach && exclusions.forEach? – me9867