当前匹配的关键字然后执行操作。如何将排除列表合并到下面的函数中?
<!-- 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
}
});
答案 0 :(得分:0)
如果文本必须是包含/排除的子串,那么
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');
}