Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中使用contains()匹配多个关键字_Javascript_Jquery_Contains - Fatal编程技术网

Javascript 在jQuery中使用contains()匹配多个关键字

Javascript 在jQuery中使用contains()匹配多个关键字,javascript,jquery,contains,Javascript,Jquery,Contains,当前正在使用contains()匹配特定关键字。如何将其扩展为包含多个关键字? 尝试使用| |(或)运算符,但没有乐趣 <!-- Product names include Apple iPad, Acer Iconia, Lenovo Thinkpad --> <h1 id="pbtitle">Apple iPad 3</h1> <div class="install_option" style="display:none;">

当前正在使用contains()匹配特定关键字。如何将其扩展为包含多个关键字? 尝试使用| |(或)运算符,但没有乐趣

<!-- Product names include Apple iPad, Acer Iconia, Lenovo Thinkpad -->

<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.exmaple.com/acc" target="_blank">
                <h3 style="font-weight:bold;">Would you like to view accessories?</h3>
            </a>    
        </div>

</div>


$(".install_option").each(function() {
    if($("h1#pbtitle:contains:contains('Acer' || 'Lenovo' || Apple )").length>0){
      $('.install_option').css('display','block');
    }
});

苹果iPad3
$(“.install_option”)。每个(函数(){
如果($(($h1#pbtitle:contains:contains('Acer'| |'Lenovo'| Apple'))。长度>0){
$('.install_option').css('display','block');
}
});
这样做

if($($($)h1#pbtitle:contains('Acer')、h1#pbtitle:contains('Lenovo')、h1#pbtitle:contains('Apple'))。长度>0)
使用JavaScript函数
工作示例


冒着被排除在问题范围之外的风险,我猜产品列表也是动态的。 在这种情况下,最好向产品标签添加一个属性,如:

<h1 id="pbtitle" has_install_option="true">Apple iPad 3</h1>

var condition = $('h1#pbtitle').attr('has_install_option') == 'true';
苹果ipad3 var条件=$('h1#pbtitle').attr('has#install_option')='true'; 并检查循环中的条件。 [想想有一天你会有一款带有安装选项的苹果产品或一款苹果产品…]


p、 s在本例中,最好将条件放在循环的外侧,因为条件相对于循环元素是静态的

,这样就完成了排除关键字(如Generic)的工作?
<h1 id="pbtitle" has_install_option="true">Apple iPad 3</h1>

var condition = $('h1#pbtitle').attr('has_install_option') == 'true';